You should write tests as you write code – not as an afterthought once the project is complete.
File naming and directory layout for tests
Test code tends to be written once and only read when the test fails. As a result, test code tends to be poorly written, with a lot of copy-pasting between test methods, which makes intent unclear.
To write clear tests:
Test one scenario per test.
Name tests, fixtures, and mocks descriptively. Do not suffix them
Use pytest.mark.parametrize to test something with different inputs (like in OCDS Kit).
Use pytest.fixture to re-use test scaffolding (like in OCDS Merge or Kingfisher Colab).
Use unittest.TestCase to re-use testing logic, including:
Test methods (like ViewTests in Toucan)
Test scaffolding, using setUp() and tearDown()
Use subTest() from
unittest(with pytest-subtests if using
pytest) to reduce duplication (like in Pelican backend).
There are important caveats to using
For Django applications:
python manage.py test
Otherwise, pytest is preferred. For applications:
pip-sync requirements_dev.txt pytest
pip install .[test] pytest