Python 使用pytest django在参数化测试之间保存数据

Python 使用pytest django在参数化测试之间保存数据,python,django,pytest-django,Python,Django,Pytest Django,我正试图用pytest.parameterize标记整理一系列故事,如下所示: 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(): settin

我正试图用pytest.parameterize标记整理一系列故事,如下所示:

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:',
        ))
    )

测试_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输出:

$ 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

作为一个非常快速的黑客,您可能能够将这两种方法结合起来

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)]]
然后在参数化标记中:

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

但是,我不知道如何从
name,count
到列表列表。也许
[姓名,计数]*len(最终故事)

我认为pytest应该在每次测试后回滚事务(刷新任何更改),但如果有黑客攻击,我就接受它!试试django_db_keepdb mark。它应该在这里测试运行后保留db
test\u database\u exists\u从上一次运行时开始
函数检查是否有可重用的db,而对于内存中的db,它总是错误的。我认为如果使用'pytest.fixture',您可以使后面的函数“依赖”在它们前面的fixture,它们会推迟拆卸自己,直到它们重新启动,所有从属项都已完成:
@pytest.mark.parametrize('name,count', final_stories)