简体   繁体   中英

keeping data between parameterized tests with pytest-django

I'm trying to put together a series of stories with the pytest.parameterize mark as such:

conftest.py:

from django.conf import settings


import pytest

@pytest.fixture(scope='session')
def django_db_modify_db_settings():
    pass


@pytest.fixture(scope='session')
def pytest_configure():
    settings.configure(
        INSTALLED_APPS=[
            'django.contrib.contenttypes',
            'django.contrib.auth',
        ],
        DATABASES=dict(default=dict(
            ENGINE='django.db.backends.sqlite3',
            NAME=':memory:',
        ))
    )

test_db.py:

import pytest
from django.contrib.auth.models import Group


@pytest.mark.parametrize('name,count', [
    ('test', 1,),
    ('staff', 2),
])
@pytest.mark.django_db(transaction=True)
def test_group(name, count):
    Group.objects.create(name=name)
    assert Group.objects.count() == count

py.test output:

$ py.test  test_db.py 
============================================ test session starts =============================================
platform linux -- Python 3.7.2, pytest-3.10.1, py-1.5.4, pluggy-0.7.1
rootdir: /home/jpic/src/djcli, inifile:
plugins: mock-1.5.0, django-3.4.2, cov-2.6.0
collected 2 items                                                                                            

test_db.py .F                                                                                          [100%]

================================================== FAILURES ==================================================
____________________________________________ test_group[staff-2] _____________________________________________

name = 'staff', count = 2

    @pytest.mark.parametrize('name,count', [
        ('test', 1,),
        ('staff', 2),
    ])
    @pytest.mark.django_db(transaction=True)
    def test_group(name, count):
        Group.objects.create(name=name)
>       assert Group.objects.count() == count
E       assert 1 == 2
E        +  where 1 = <bound method BaseManager._get_queryset_methods.<locals>.create_method.<locals>.manager_method of <django.contrib.auth.models.GroupManager object at 0x7f351e01ef98>>()
E        +    where <bound method BaseManager._get_queryset_methods.<locals>.create_method.<locals>.manager_method of <django.contrib.auth.models.GroupManager object at 0x7f351e01ef98>> = <django.contrib.auth.models.GroupManager object at 0x7f351e01ef98>.count
E        +      where <django.contrib.auth.models.GroupManager object at 0x7f351e01ef98> = Group.objects

test_db.py:12: AssertionError

As you can see, the first test passes which means that one group was created and that one group was left.

In the second test you can see the test fail because the first group is gone.

This implementation works, but we have less detail in the summary because this groups the tests into one.

import pytest
from django.contrib.auth.models import Group


story = [
    ('test', 1,),
    ('staff', 2),
]
@pytest.mark.django_db(transaction=True)
def test_group():
    for name, count in story:
        Group.objects.create(name=name)
        assert Group.objects.count() == count

As a very quick hack, you might be able to combine the two methods.

stories = [
    ('test', 1,),
    ('staff', 2),
]

param_story = []
final_stories = []

for story in stories:
    param_story.append(story)
    append_this = list(param_story)        
    final_stories.append(append_this)

print(final_stories)
>>> [[('test', 1)], [('test', 1), ('staff', 2)]]

Then in the paramaterize mark:

@pytest.mark.parametrize('name,count', final_stories)

I'm not sure how to go from name,count to a list of lists, however. Perhaps [name, count] * len(final_stories) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM