Skip to content

service

dev_tool.services.unittest.service

log = logging.getLogger(__name__) module-attribute

UnitTestService

Bases: BaseService

A service class for running unit tests.

This class provides methods for running tests for both Django and Python projects.

The constructor for the UnitTestService class.

Parameters:

  • config (dict[str, Any]) –

    The configuration dictionary for unit test settings.

Source code in dev_tool/services/unittest/service.py
def __init__(self, config: dict[str, Any]) -> None:
    """
    The constructor for the UnitTestService class.

    :param config: The configuration dictionary for unit test settings.
    """

    super().__init__()

    self.set_config(config or {})

set_config

A method that sets the configuration for the unit test service.

Parameters:

  • config (dict[str, Any]) –

    The configuration dictionary containing test settings.

Source code in dev_tool/services/unittest/service.py
def set_config(self, config: dict[str, Any]) -> None:
    """
    A method that sets the configuration for the unit test service.

    :param config: The configuration dictionary containing test settings.
    """

    self.config = config
    self.apps = self.config.get('apps', ['.'])
    self.settings = self.config.get('settings', 'system.testing.settings')
    self.failfast = self.config.get('failfast', False)
    self.keepdb = self.config.get('keepdb', True)

run_django_unittest

A method that runs unit tests for Django projects.

This method requires Django settings to be properly configured.

Source code in dev_tool/services/unittest/service.py
@is_settings
def run_django_unittest(self) -> None:
    """
    A method that runs unit tests for Django projects.

    This method requires Django settings to be properly configured.
    """

    if not self.apps:
        message = 'No apps to test.'
        self.notification.warning_text(message)

        log.debug(message)
        return

    self._validate_directories(self.apps, DjangoTestError)

    message = 'Running Django unit tests...'
    self.notification.normal_text(message)

    apps = list(self.apps) if self.apps else '.'

    command = [
        'manage.py',
        'test',
        *apps,
        f'--settings={self.settings}',
        '-v', '2'
    ]

    if self.failfast:
        command.append('--failfast')

    if self.keepdb:
        command.append('--keepdb')

    try:
        subprocess.run(self.with_venv(command), check=False)
    except Exception:
        message = 'Failed to run Django unit tests'
        log.exception(message)

        raise DjangoTestError(message) from None

run_python_unittest

A method that runs unit tests for Python projects.

Source code in dev_tool/services/unittest/service.py
def run_python_unittest(self) -> None:
    """A method that runs unit tests for Python projects."""

    if not self.apps:
        message = 'No test directory specified.'
        self.notification.warning_text(message)

        log.debug(message)
        return

    self._validate_directories(self.apps, PythonTestError)

    message = 'Running Python unit tests...'
    self.notification.normal_text(message)

    apps = list(self.apps)

    command = [
        '-m',
        'unittest',
        'discover',
        '-s', *apps,
        '-v'
    ]

    if self.failfast:
        command.append('--failfast')

    try:
        subprocess.run(self.with_venv(command), check=False)
    except Exception:
        message = 'Failed to run Python unit tests'
        log.exception(message)

        raise PythonTestError(message) from None

with_venv

A method that prepends the virtual environment Python to a command.

Parameters:

  • command (list[str]) –

    The command to modify.

Returns:

  • list[Path | str]

    The modified command with virtual environment Python.

Source code in dev_tool/services/unittest/service.py
def with_venv(self, command: list[str]) -> list[Path | str]:
    """
    A method that prepends the virtual environment Python to a command.

    :param command: The command to modify.
    :return: The modified command with virtual environment Python.
    """

    return [VENV_PYTHON, *command]