I use a mixer to create my model and run them with pytest. All I want to do is generate my model only once and run several tests with the same model that I generate.
Here is what I want:
the code
import pytest
from mixer.backend.django import mixer
from weddings.models import Wedding
@pytest.mark.django_db
class TestProductApi(object):
@pytest.fixture(scope="module")
def wedding(self):
wedding = mixer.blend(
'weddings.Wedding',
)
mixer.cycle(12).blend(
'weddings.Product',
wedding=wedding,
)
mixer.cycle(4).blend(
'weddings.Product',
wedding=wedding,
is_visible=False,
)
mixer.blend(
'weddings.Product',
wedding=wedding,
is_active=False,
)
return wedding
def test_number_one(self, wedding):
print 'running test_number_one'
print 'wedding.id == {}'.format(wedding.id)
print 'Wedding.objects.all().count() == {}'.format(Wedding.objects.all().count())
print 'finished test_number_one'
def test_number_two(self, wedding):
print 'running test_number_two'
print 'wedding.id == {}'.format(wedding.id)
print 'Wedding.objects.all().count() == {}'.format(Wedding.objects.all().count())
print 'finished test_number_two'
Output
tests/weddings/api/test_products.pyTestProductApi.test_number_one 0% running test_number_one
wedding.id == 1
Wedding.objects.all().count() == 1
finished test_number_one
tests/weddings/api/test_products.pyTestProductApi.test_number_one ✓ 50% █████
tests/weddings/api/test_products.pyTestProductApi.test_number_two 50% █████ running test_number_two
wedding.id == 1
Wedding.objects.all().count() == 0
finished test_number_two
What I do in my tests is not so important, however I want all of them to start with the same created object wedding. When I run my tests, every time I create a test, a new one is created wedding.
Here is the version of what I'm using:
pytest==2.7.1
pytest-django==2.9.1
Django==1.8.8
mixer==5.3.1
python==2.7.6
EDIT 1 . I also tried to add an area to my device, but this only works for the first test. If I make a request in the second test, the model does not exist, even if the device works fine.
2: scope="module" .