簡體   English   中英

使用 pytest-django 在參數化測試之間保存數據

[英]keeping data between parameterized tests with 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():
    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 輸出:

$ 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

如您所見,第一個測試通過,這意味着創建了一個組,剩下一個組。

在第二個測試中,您可以看到測試失敗,因為第一組消失了。

此實現有效,但我們在摘要中的詳細信息較少,因為這將測試分組為一個。

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

作為一個非常快速的技巧,您可以將這兩種方法結合起來。

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到列表列表。 也許[name, count] * len(final_stories)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM