我如何在TestCase
方法?类似问题的几个答案似乎暗示我的例子应该有效:
import pytest
from django.test import TestCase
from myapp.models import Category
pytestmark = pytest.mark.django_db
@pytest.fixture
def category():
return Category.objects.create()
class MyappTests(TestCase):
def test1(self, category):
assert isinstance(category, Category)
但这总是会导致错误:
TypeError: test1() missing 1 required positional argument: 'category'
我意识到我可以将这个简单的示例转换为一个函数,并且它会起作用。我更喜欢使用 django 的TestCase
因为它包括对导入传统“Django 装置”文件,我的几个测试需要这些文件。将我的测试转换为函数需要重新实现此逻辑,因为没有记录的导入方式“Django 装置”使用 pytest (或 pytest-django)。
软件包版本:
Django==3.1.2
pytest==6.1.1
pytest-django==4.1.0
我发现使用“usefixtures”方法更容易。它没有显示该函数神奇的第二个参数,并且它明确标记该类具有固定装置。
@pytest.mark.usefixtures("category")
class CategoryTest(TestCase):
def test1(self):
assert Category.objects.count() == 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)