【发布时间】:2015-03-11 09:10:00
【问题描述】:
此测试会话正常工作:
from myapp.models import MyModel
@pytest.fixture(scope='function')
def mymodel():
return G(MyModel)
@pytest.mark.django_db
def test_mymodel_one(mymodel):
assert MyModel.objects.count() > 0
@pytest.mark.django_db
def test_mymodel_two(mymodel):
assert MyModel.objects.count() > 0
并产生这个输出:
========= test session starts =========
tests/myapp/test_pp.py::test_mymodel_one PASSED
tests/myapp/test_pp.py::test_mymodel_two PASSED
但如果我将夹具的范围更改为“会话”,则测试二会失败:
========= test session starts =========
tests/myapp/test_pp.py::test_mymodel_one PASSED
tests/myapp/test_pp.py::test_mymodel_two FAILED
========= FAILURES ==============
_________ test_mymodel_two ________
tests/myapp/test_pp.py:85: in test_mymodel_two
assert MyModel.objects.count() > 0
E assert 0 > 0
创建的对象正确地从夹具返回(我可以访问他的值),但它不再存储。 如何使用会话范围并维护测试数据库中的存储?
【问题讨论】:
-
遇到了同样的问题,当我给我的fixtures模块范围并且在测试类中有一个
teardown_method时。teardown_method删除了一些由灯具创建的文件。显然scope是运行或不运行teardown和setup方法的决定性因素,即使它们只属于test class并且fixtures在test class之外,被@987654332 使用@.
标签: python django testing pytest