Back to Repositories

Testing User Experience Components in Sherlock Project

This test suite validates user experience functionality in the Sherlock project, focusing on NSFW content filtering, username pattern handling, and CLI argument validation. The tests ensure proper behavior of core user-facing features and error handling.

Test Coverage Overview

The test suite provides comprehensive coverage of user experience features including NSFW site filtering, username pattern expansion, and command-line interface validation. Key functionality tests verify NSFW content removal with explicit site selection and wildcard username parameter handling. Edge cases include invalid CLI arguments and malformed username patterns.

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator for data-driven testing of NSFW site filtering and CLI argument validation. The implementation follows AAA (Arrange-Act-Assert) pattern with clear test isolation. Pytest fixtures are used for test data setup and dependency injection.

Technical Details

  • Testing Framework: pytest
  • Key Classes: Interactives, sherlock
  • Custom Exceptions: InteractivesSubprocessError
  • Test Fixtures: sites_obj
  • Parametrization: Used for NSFW sites and CLI arguments

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test isolation, descriptive test names, and effective use of pytest features. The code organization follows single responsibility principle with each test focusing on a specific feature. Exception handling is properly tested and assertions are clear and specific.

sherlock-project/sherlock

tests/test_ux.py

            
import pytest
from sherlock_project import sherlock
from sherlock_interactives import Interactives
from sherlock_interactives import InteractivesSubprocessError

def test_remove_nsfw(sites_obj):
    nsfw_target: str = 'Pornhub'
    assert nsfw_target in {site.name: site.information for site in sites_obj}
    sites_obj.remove_nsfw_sites()
    assert nsfw_target not in {site.name: site.information for site in sites_obj}


# Parametrized sites should *not* include Motherless, which is acting as the control
@pytest.mark.parametrize('nsfwsites', [
    ['Pornhub'],
    ['Pornhub', 'Xvideos'],
])
def test_nsfw_explicit_selection(sites_obj, nsfwsites):
    for site in nsfwsites:
        assert site in {site.name: site.information for site in sites_obj}
    sites_obj.remove_nsfw_sites(do_not_remove=nsfwsites)
    for site in nsfwsites:
        assert site in {site.name: site.information for site in sites_obj}
        assert 'Motherless' not in {site.name: site.information for site in sites_obj}

def test_wildcard_username_expansion():
    assert sherlock.check_for_parameter('test{?}test') is True
    assert sherlock.check_for_parameter('test{.}test') is False
    assert sherlock.check_for_parameter('test{}test') is False
    assert sherlock.check_for_parameter('testtest') is False
    assert sherlock.check_for_parameter('test{?test') is False
    assert sherlock.check_for_parameter('test?}test') is False
    assert sherlock.multiple_usernames('test{?}test') == ["test_test" , "test-test" , "test.test"]


@pytest.mark.parametrize('cliargs', [
    '',
    '--site urghrtuight --egiotr',
    '--',
])
def test_no_usernames_provided(cliargs):
    with pytest.raises(InteractivesSubprocessError, match=r"error: the following arguments are required: USERNAMES"):
        Interactives.run_cli(cliargs)