我最近从Django的TestCase类切换到了第三方pytest系统.这使我可以极大地加快测试套件的速度(提高了5倍),并且总体而言是很棒的经验.
我确实有硒问题.我做了一个简单的测试,将浏览器包含在测试中
@pytest.yield_fixture
def browser(live_server, transactional_db, admin_user):
driver_ = webdriver.Firefox()
driver_.server_url = live_server.url
driver_.implicitly_wait(3)
yield driver_
driver_.quit()
但是由于某种原因,测试之间数据库没有正确重置.我有一个类似的测试
class TestCase:
def test_some_unittest(db):
# Create some items
#...
def test_with_selenium(browser):
# The items from the above testcase exists in this testcase
在test_some_unittest中创建的对象存在于test_with_selenium中.我不太确定如何解决这个问题.
解决方法:
从django.test.TestCase切换为pytest意味着要使用pytest-django插件,并且您的测试应如下所示:
class TestSomething(object):
def setup_method(self, method):
pass
@pytest.mark.django_db
def test_something_with_dg(self):
assert True
最重要的是,它没有django.test.TestCase(它是从python std unittest框架派生的)继承.
@ pytest.mark.django_db表示您的测试用例将在事务中运行,该事务将在测试用例结束后回滚.
第一次出现django_db标记也会触发django迁移.
当心特殊的pytest方法(例如setup_method)中使用数据库调用,因为它不受支持并且存在其他问题:
django-pytest setup_method database issue
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。