14.14. Testing the server code

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 camcops_server/cc_modules/cc_patient.py is tested in camcops_server/cc_modules/tests/cc_patient_tests.py.

Test classes should end in Tests e.g. PatientTests. Tests that require an empty database should inherit from DemoRequestTestCase. Tests that require the demonstration database should inherit from DemoDatabaseTestCase. See camcops_server/cc_modules/cc_unittest. Tests that do not require a database can just inherit from the standard python unittest.TestCase

To run all tests whilst in the CamCOPS virtual environment:

cd server/camcops_server

By default if there is an existing test database, this will be reused.

Custom CamCOPS pytest options:


Create a new test database. Necessary when there have been schema changes.


Store the database in memory instead of on disk (SQLite only).


Log all SQL statements to the default log handler


Use MySQL instead of the default SQLite


SQLAlchemy test database URL (MySQL only, default: mysql+mysqldb://camcops:camcops@localhost:3306/test_camcops?charset=utf8

Some common standard pytest options:


Stop on failure

-k wildcard

Run tests whose classes or files only match the wildcard


Do not capture stdout and stderr. Necessary when debugging with e.g. pdb


Run previously failed tests first