I have the following set up on Ubuntu 14.04:
- python 2.7.6
- django 1.7 [though I reproduced the same behaviour with django 1.9 too]
- pytest-django 2.8.0 [also tested with 2.9.1]
- pytest 2.7.2 [also tested with 2.8.3]
and the following test code:
import pytest
from django.db import connection
import settings
from pollsapp.models import Question
original_db_name = settings.DATABASES["default"]["NAME"]
@pytest.mark.django_db
class TestExperiment(object):
def setup_method(self, method):
# it's not using the "test_" + DATABASE_NAME !
assert connection.settings_dict["NAME"] == \
settings.DATABASES["default"]["NAME"]
Question.objects.create(question_text="How are you?")
# this data remains in the main database
-
Although the class is marked to use django database, the data created in the constructor arrive in the main (production) database (name taken from settings.py)
-
Putting the django_db decorator above the setup_method does not make any difference
-
This data created in the setup_method remain in the main database, are not rolled back as they should be and as they would be if the data creation call was made in the test_case method
-
This behaviour happens when the test is run on its own. When running it within the test suite, the setup_method db calls fails with: Failed: Database access not allowed, use the "django_db" mark to enable although the decorator is clearly there (which means that this error message is not to be 100% trusted btw).
pytest is an awesone framework and django-pytest works great if database calls happen from django_db marked test case methods.
It looks like no db interaction should ever be present in special pytest methods such as setup_method, teardown_method, etc. Although the documentation doesn't say anything about it:
I am getting this behaviour with both Django 1.7 as well as with 1.9 (latest stable).
Here is the link to the whole test module: http://ift.tt/1jDiB2j
Thanks very much for your answers / thoughts.
Zdenek
Aucun commentaire:
Enregistrer un commentaire