[Django]-Django + tox: Apps aren't loaded yet

2👍

Django doesn’t know how to run pytest-style test functions. Plain pytest also doesn’t provide a django integration. You can either

  • write a custom test runner (a minimal example):

    # myapp/runner.py
    
    class PytestRunner:
        def run_tests(self, test_labels):
            import pytest    
            return pytest.main(test_labels)
    

    and configure your app to use it instead of the default DiscoverRunner:

    # myapp/settings.py
    TEST_RUNNER = 'myapp.runner.PytestRunner'
    

    Now python manage.py test will invoke pytest instead of the default unittest.

  • Or use the pytest-django plugin. Add it to deps:

    deps =
        ...
        pytest
        pytest-django
        ...
    

    This doesn’t provide integration with manage.py test per se, but you will be able to invoke tests as usual via pytest:

    $ pytest --ds=myapp.settings
    ...
    

    The plugin also provides many useful fixtures that reimplement Django’s test helpers (like RequestFactory, Client etc) so you can do a complete port of the unittest-style test classes to pytest-style test functions. Refer to pytest-django docs for configurations details and code examples.

Leave a comment