13.1. Testing
The python code on the server is tested with pytest
Tests are kept separate to the code they are testing in a tests
sub-folder
with the filename of the module appended with _tests.py
. So the module
crate_anon/anonymise/anonymise.py
is tested in
crate_anon/anonymise/tests/anonymise_tests.py
.
Test classes should end in Tests
e.g. AnonRegexTests
. Tests that require
an empty database should inherit from DatabaseTestCase
. See
crate_anon/testing/classes
. Tests that do not require a database can just
inherit from the standard python unittest.TestCase
To run all tests whilst in the CRATE virtual environment:
cd crate_anon pytest
By default if there is an existing test database, this will be reused.
Custom CRATE pytest options:
- --create-test-db
Create a new test database. Necessary when there have been schema changes. Note that the option
--create-db
is added by pytest-django and isn’t currently used by CRATE.- --database-in-memory
Store the database in memory instead of on disk (SQLite only).
- --echo
Log all SQL statements to the default log handler
- --db-url
SQLAlchemy test database URL (Not applicable to SQLite).
Some common standard pytest options:
- -x
Stop on failure
- -k wildcard
Run tests whose classes or files only match the wildcard
- -s
Do not capture stdout and stderr. Necessary when debugging with e.g. pdb
- --ff
Run previously failed tests first