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. A number of
unittest.TestCase subclasses are defined in
camcops_server/cc_modules/cc_unittest.
Tests that require an empty database and a request object should inherit from
DemoRequestTestCase.Tests that require a minimal database setup (system user, superuser set on the request object, group administrator and a server device) should inherit from
BasicDatabaseTestCase.Tests that require the demonstration database, which has a patient and two instances of each type of task should inherit from
DemoDatabaseTestCase.Tests that do not require a database can just inherit from the standard python
unittest.TestCase.
Use Factory Boy test factories to create test instances of SQLAlchemy
database models. See camcops_server/cc_modules/cc_testfactories.py and
camcops_server/tasks/tests/factories.py.
To run all tests whilst in the CamCOPS virtual environment:
cd server/camcops_server pytest
By default if there is an existing test database, this will be reused.
Custom CamCOPS pytest options:
- --create-db
Create a new test database. Necessary when there have been schema changes.
- --database-in-memory
Store the database in memory instead of on disk (SQLite only).
- --echo
Log all SQL statements to the default log handler
- --mysql
Use MySQL instead of the default SQLite
- --db-url
SQLAlchemy test database URL (MySQL only, default: mysql+mysqldb://camcops:camcops@localhost:3306/test_camcops?charset=utf8
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