"Apps aren't loaded yet" when trying to run pytest-django
Out of the box, pytest
doesn't know about the Django database, even with pytest-django
installed. Never fear, though: pytest-django
makes it easy for your tests to access the Django database using its django_db pytest mark.
Give this a try:
import pytest
@pytest.mark.django_db
def test_was_published_recently_with_future_question():
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
assert future_question.was_published_recently() is False
I had a similar problem when invoking tests either with pytest
or python setup.py test
.
For pytest
invocation installing pytest-django
in my virtual env solved the problem.
For python setup.py install
adding pytest-django
to the tests_require
argument of setup()
solved it.
Here's the snippet of setup.py
:
TEST_REQUIREMENTS = [
'pytest',
'pytest-django',
'pylint',
'pylint_django',
'git-pylint-commit-hook',
]
setup(
name='foo',
version='0.0.1',
description='Foo package',
author='...',
author_email='...',
packages=['foo'],
install_requires=INSTALL_REQUIREMENTS,
setup_requires=SETUP_REQUIREMENTS,
tests_require=TEST_REQUIREMENTS,
)
According to Django: AppRegistryNotReady(), when not using manage.py
one must call django.setup()
explicitly. I verified this by running the pytest
test from a manage.py
shell:
Kurts-MacBook-Pro:mysite2 kurtpeek$ python3 manage.py shell
Python 3.6.3 (v3.6.3:2c5fed86e0, Oct 3 2017, 00:32:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import pytest
In [2]: pytest.main('polls/tests.py')
================================= test session starts ==================================
platform darwin -- Python 3.6.3, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /Users/kurtpeek/Documents/Scratch/mysite2, inifile: pytest.ini
plugins: timeout-1.2.1
collected 1 item
polls/tests.py F
======================================= FAILURES =======================================
___________________ test_was_published_recently_with_future_question ___________________
def test_was_published_recently_with_future_question():
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
> assert future_question.was_published_recently() is False
E assert True is False
E + where True = <bound method Question.was_published_recently of <Question: >>()
E + where <bound method Question.was_published_recently of <Question: >> = <Question: >.was_published_recently
polls/tests.py:18: AssertionError
=================================== warnings summary ===================================
None
passing a string to pytest.main() is deprecated, pass a list of arguments instead.
-- Docs: http://doc.pytest.org/en/latest/warnings.html
========================= 1 failed, 1 warnings in 0.14 seconds =========================
Out[2]: 1
This is not really an acceptable solution, however, as the tests need to be runnable from the command line. Are there perhaps other pytest
decorators to ensure the required setup?