pytest support

While django-app-helper was born with Django TestCase in mind, it can be used with pytest with some configuration together with pytest-django.

django-app-helper runner

You can run pytest tests by using a custom runner (based on pytest-django documentation); to enable it, add the following to project helper.py file:

HELPER_SETTINGS = {
    ...
    "TEST_RUNNER": "app_helper.pytest_runner.PytestTestRunner",
    ...
}

Using this approach you can mix pytest tests and Django TestCase ones, the runner will take care of discovering and running both.

Running tests

Invoke app_helper as usual:

$ python helper.py <app-name> test

pytest options

The runner support translates the following Django test runner options to pytest ones:

  • verbosity == 0: --quiet

  • verbosity == 2: --verbose

  • verbosity == 3: -vv

  • failfast: --exitfirst

  • keepdb: --reuse-db

All the other pytest and pytest plugins are supported either via PYTEST_ARGS enviroment variable or --runner-options cmdline argument.

Environment variable example:

PYTEST_ARGS='-s -k my_test' python helper.py test

argument variable example:

python helper.py test --runner-options="-k my_test"

In case arguments are passed via both channels they are merged together, with runner-options arguments having priority over environment variables in case of overlapping options.

standard pytest

Running tests

Invoke pytest as usual:

$ python -mpytest <args>

or:

$ pytest <args>

In this case you don’t need any special syntax to pass commands as the django-app-helper pytest runner is not executed and pytest is full in control.

Using BaseTestCaseMixin

While its BaseTestCaseMixin is built on Django TestCase, it can be used in pytest classes:

Fixtures, markers and decorators can be used as usual on test methods as in classic pytest classes.

class TestTags(BaseTestCaseMixin):
    ...
    def test_foo(self):
        ...