Testing is an important part of maintaining software quality and Portal encourages the use of automated testing. In order to get started with the test runner you have to begin by installing the required dependencies. This can be done using the following command:
# /opt/cantemo/python/bin/pip install -r /opt/cantemo/portal/test-requirements.pip
This will download and install a number of python modules required for testing. It is recommended you do that on a separate system from your production system.
The test runner will create a database and the necessary tables for testing, but in order to allow that you need to give the portal user in postgresql the role CREATEDB:
# sudo -u postgres psql portal -c 'alter USER portal CREATEDB;'
After this, you can run the test runner on the entire test suite using:
# python /opt/cantemo/portal/manage.py test
Individual modules can be tested like so:
# /opt/cantemo/portal/manage.py test portal.plugins.yourappname
2.1. Code layout¶
The recommended module structure is to create a submodule called tests where you keep the test files for that module. This means that if your app is called yourappname then you should have a folder called /opt/cantemo/portal/portal/plugins/yourappname/tests with an empty file called __init__.py and test files with filenames starting with the string test_. The included app template command start_portal_app will create an example structure for you.
2.2. Code coverage¶
If you have installed test-requirements.pip you can also benefit from coverage.py showing what code was executed when testing, and which lines are missed. You can extract code coverage from the test runner using:
# /opt/cantemo/portal/manage.py test --with-coverage
Or if you only want to measure coverage on your own app:
# /opt/cantemo/portal/manage.py test --with-coverage --cover-package=portal.plugins.yourappname portal.plugins.yourappname