Skip to content
Jacob deGroot-Maggetti edited this page Apr 26, 2024 · 13 revisions

CantusDB has several test suites that should be run and kept up-to-date: one for the users app, one for the articles app, and two for the main_app app. When writing tests, we use the coverage library to ensure that our tests are testing everything we want them to test (see Test Coverage section, below).

Tests for main_app

Tests for the main_app app are kept in main_app/tests (directory)

There are three test suites that should be run:

  • test_models.py (code)
    • run with python -Wa manage.py test main_app.tests.test_models (the -Wa flag tells Python to display deprecation warnings)
  • test_views.py (code)
    • run with python -Wa manage.py test main_app.tests.test_views
  • test_functions.py (code)
    • run with python -Wa manage.py test main_app.tests.test_functions
  • you can run all three of these test suites at once using this command:
    • python manage.py test main_app.tests.test_models main_app.tests.test_views main_app.tests.test_functions articles.tests.test_articles users.tests

main_app/tests contains two additional files:

  • make_fakes.py (code): contains a number of functions used in the tests in test_models.py and test_views.py
  • test_input.py (code): This test suite is out-of-date and should not be run. It contains a few failing tests of the Chant Create view; eventually, these tests might be fixed and added to test_views.py

Tests for articles

Tests for the articles app are kept in articles/tests (directory).

There is one test suite that should be run:

  • test_articles.py (code)
    • run with python -Wa manage.py test articles.tests.test_articles

Tests for users

Tests for the users app are kept in users/tests.py (code).

There is one test suite that should be run:

  • tests.py (code)
    • run with python -Wa manage.py test users.tests

Test coverage

Whenever the codebase changes, in addition to simply running the tests to ensure no failures/errors have emerged, it's also a good idea to check our level of test coverage. In any of the commands above, you can substitute coverage run for python to have the coverage package keep track of which lines of code in the project are executed. Following this, you can run coverage report to have a summary printed to the terminal. Or, you can run coverage html to have a thorough report created as html files - these can then be viewed in a browser. In summary, to run all the tests and view a report in your browser, run

$ coverage run manage.py test main_app.tests
Found 328 test(s).
Creating test database for alias 'default'...
# ...a bunch of output will be printed to the terminal...
Destroying test database for alias 'default'... 
$ coverage html # a collection of html files will be created in an `htmlcov` directory

To view them, in your browser, visit path/to/CantusDB/django/cantusdb_project/htmlcov/.