Bases: BaseService
A service class for running unit tests.
This class provides methods for running tests for Django, Python, and JavaScript projects.
The constructor for the UnitTestService class.
Parameters:
-
config
(dict[str, Any])
–
The configuration dictionary for unit test settings.
-
runner
(CommandRunnerProtocol)
–
The command runner for executing commands.
Source code in dev_tool/services/unittest/service.py
| def __init__(self, config: dict[str, Any], runner: CommandRunnerProtocol) -> None:
"""
The constructor for the UnitTestService class.
:param config: The configuration dictionary for unit test settings.
:param runner: The command runner for executing commands.
"""
super().__init__()
self._runner = runner
self.set_config(config or {})
|
A property that returns the command runner.
Returns:
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)
|
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.
"""
"""A method that runs unit tests for Python projects (including Django)."""
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 Django unit tests...'
self.notification.normal_text(message)
command = ['-m', 'pytest', *self.apps, '-v']
if self.failfast:
command.append('-x')
try:
self.runner.run_python_command(command, check=True)
except Exception as exception:
message = 'Failed to run Python unit tests'
log.exception(message)
raise PythonTestError(message) from exception
|
A method that runs JavaScript unit tests.
Source code in dev_tool/services/unittest/service.py
| def run_javascript_unittest(self) -> None:
"""A method that runs JavaScript unit tests."""
message = 'Running JavaScript unit tests...'
self.notification.normal_text(message)
apps = self.config.get('js_apps', ['tests/js'])
command = ['test', *apps]
if self.failfast:
command.append('--bail')
try:
self.runner.run_bun_command(command, check=False)
except Exception as exception:
message = 'Failed to run JavaScript unit tests'
log.exception(message)
raise JavascriptTestError(message) from exception
|
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:
self.runner.run_python_command(command, check=True)
except Exception as exception:
message = 'Failed to run Python unit tests'
log.exception(message)
raise PythonTestError(message) from exception
|
A method that prepends the virtual environment Python to a command.
Parameters:
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]
|