Testing Mastoscore¶
This section covers the testing framework and practices used in the Mastoscore project.
Overview¶
Mastoscore uses pytest as its testing framework. The tests are organized in the tests/
directory at the root of the project. The test suite includes unit tests for individual modules and integration tests that verify the interaction between different components.
See subsections: * Writing Tests * Test Coverage
Running Tests¶
To run the tests, you can use any of the following commands from the project root directory:
# Run all tests
poetry run pytest
# Run specific test files
python -m pytest tests/test_config.py
# Run specific test functions
python -m pytest tests/test_config.py::test_read_config_with_valid_file
Test Structure¶
The test suite is organized as follows:
tests/conftest.py
: Contains shared fixtures used across multiple test filestests/test_*.py
: Individual test files for each module
Fixtures¶
Fixtures are defined in conftest.py
and provide reusable test data and objects. Key fixtures include:
sample_config
: Creates a sample configuration for testingtemp_directory
: Creates a temporary directory for test filessample_toot_data
: Provides sample toot data for testingsample_journal_files
: Creates sample journal files in a temporary directory
Writing Tests¶
When writing tests for Mastoscore, follow these guidelines:
- Test one thing at a time: Each test function should focus on testing a single aspect of functionality.
- Use descriptive names: Test function names should clearly describe what they're testing.
- Use fixtures: Leverage the fixtures in
conftest.py
to avoid duplicating setup code. - Mock external dependencies: Use
pytest-mock
to mock external services like the Mastodon API. - Test edge cases: Include tests for error conditions and edge cases.
Example Test¶
def test_check_journaldir_existing_directory(temp_directory):
"""Test check_journaldir with an existing directory."""
# The temp_directory fixture creates a directory that exists
result = check_journaldir(temp_directory)
assert result is True