Consider next required conditions for running tests:
-
venv
Don't forget to run it under configured venv. Look setup venv how to configure venv
-
postgres
Due to tests make database queries the local postgres should be running.
Run postgres:
$ docker-compose -f docker-compose.yml up -d postgres
For first time run migrations (it needs only for fresh images)
(venv) $ ./manage.py migrate
-
redis Run redis:
$ docker-compose -f docker-compose.yml up -d redis
-
build frontend
For views tests its essential to build our frontend upfront. Hot to build front look setup-frontend section, for now just run next commands:
$ cd frontend $ npm ci # or npm install $ npm run build
Above commands will create required
webpack-stats.json
file -
test environment variables
DJANGO_SETTINGS_MODULE=club.settings; PYTHONUNBUFFERED=1; TESTS_RUN=da POSTGRES_DB=rationalanswer POSTGRES_USER=postgres POSTGRES_PASSWORD=postgres POSTGRES_HOST=localhost REDIS_DB=0 REDIS_HOST=localhost
-
remove all the feature flag variables from the
.env
file
Basically tests automatically runs in CI in opened PR, but if you want to run tests locally there are few ways to do it
- virgin shell
$ make test
- venv shell
(^don't forget inject test env variables)
$ source {your-venv-folder}/bin/activate (venv) $ ./manage.py test
- pycharm profession edition
Use
django tests
template out of the box - pycharm common edition
- Make sure you have set
Unittest
as default test runner: Settings --> Tools --> Python Integrated Tools --> Default Test Runner: Unittests - In Run/Debug Configuration put environment variables from prerequisites
- For workaround "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet." add this lines to test file before importing models:
import django django.setup()
- Make sure you have set
For more information about testing in django look well written official documentation