Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTest with single django model creation

I'm using mixer to generate my model and running them with pytest. All I wanna do is generate my model only one time and run several tests with the same model I generate.

Here is what I want:

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'm doing in my tests are not that important, however, I want all of them to run with the same generated wedding object. When I run my tests, a new wedding is created every time I create a test.

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 adding a scope to my fixture but that work's only for the first test. If I make a query in the second test, the model doesn't exist, even thou the fixture works just fine.

EDIT 2: Inserting the scope="module" to the fixture and showing the result of the ran tests.

like image 426
jarussi Avatar asked Nov 08 '22 18:11

jarussi


1 Answers

This worked for me. Give it a try...

import pytest
from mixer.backend.django import mixer
from weddings.models import Wedding

@pytest.fixture(scope="class")
def wedding(self):
    wedding_obj = mixer.blend(
        'weddings.Wedding',
    ) 
    mixer.cycle(12).blend(
        'weddings.Product',
        wedding=wedding_obj,
    ) 

    mixer.cycle(4).blend(
        'weddings.Product',
        wedding=wedding_obj,
        is_visible=False,
    ) 

    mixer.blend(
        'weddings.Product',
        wedding=wedding_obj,
        is_active=False,
    ) 
    return wedding_obj

@pytest.mark.django_db
@pytest.mark.usefixtures("wedding")
class TestProductApi(object):

    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'
like image 193
Ashish Saini Avatar answered Nov 15 '22 04:11

Ashish Saini