3

I have a Django project in which certain environment variables are set in manage.py which later serve as the values defined in settings.py. Therefore, in order to run pytest-django, I'd like to run manage.py first.

I'm trying to follow the instructions in https://pytest-django.readthedocs.io/en/latest/faq.html#how-can-i-use-manage-py-test-with-pytest-django, but I'm running into an unexpected error. I have the following directory structure:

.
├── lucy
│   ├── settings
    │   ├── base.py
    │   ├── development.py
    │   ├── production.py
│   └── staging.py
│   ├── staticfiles
│   ├── urls.py
│   └── wsgi.py
├── lucy_web
│   ├── __init__.py
│   ├── actions.py
│   ├── admin
│   ├── apps.py
│   ├── fixtures
│   ├── forms
│   ├── lib
│   ├── management
│   ├── migrations
│   ├── models
│   ├── runner.py
│   ├── serializers.py
│   ├── static
│   ├── templates
│   ├── templatetags
│   ├── tests
│   ├── urls.py
│   └── views
├── manage.py
├── pytest.ini

The contents of runner.py are taken exactly from the FAQ:

class PytestTestRunner(object):
    """Runs pytest to discover and run tests."""

    def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs):
        self.verbosity = verbosity
        self.failfast = failfast
        self.keepdb = keepdb

    def run_tests(self, test_labels):
        """Run pytest and return the exitcode.

        It translates some of Django's test command option to pytest's.
        """
        import pytest

        argv = []
        if self.verbosity == 0:
            argv.append('--quiet')
        if self.verbosity == 2:
            argv.append('--verbose')
        if self.verbosity == 3:
            argv.append('-vv')
        if self.failfast:
            argv.append('--exitfirst')
        if self.keepdb:
            argv.append('--reuse-db')

        argv.extend(test_labels)
        return pytest.main(argv)

Finally, in lucy/settings/base.py I add the following line:

TEST_RUNNER = 'lucy_web.runner.PytestTestRunner'

Finally, the pytest.ini file is as in the example in the docs:

# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = lucy.settings.production
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py

The problem is that pytest doesn't seem able to find the tests. If I run the command

python manage.py test lucy_web.tests

I get

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py test lucy_web.tests
============================================ test session starts ============================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/kurtpeek/Documents/Dev/lucy/lucy-web, inifile: pytest.ini

======================================= no tests ran in 0.00 seconds ========================================
ERROR: file not found: lucy_web.tests

However, if I comment out the TEST_RUNNER line in base.py and run the same command, the tests run successfully:

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py test lucy_web.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
....E.F................
----------------------------------------------------------------------
Ran 23 tests in 15.974s

FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...

What am I doing wrong here? Do I need to put runner.py in a different location?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

1 Answers1

1

Maybe it's because of __init__.py

I experienced a similar situation when using pytest. I deleted __init__.py in tests dir and it tested well.

It's little bit different situation, but I hope this helps.

links that I got help.

https://docs.pytest.org/en/latest/pythonpath.html

`py.test` and `__init__.py` files

Community
  • 1
  • 1
seuling
  • 2,850
  • 1
  • 13
  • 22