我试图与pytest一起运行我的django项目,并执行固定设置/拆卸。
我试图使用yield
遵循最佳练习,但我遇到了一个错误:
--- ERROR at setup of test_browsing_check ---
pytest.fixture functions cannot use ``yield``. Instead write and return an inner function/generator and let the consumer call and iterate over it.:
@pytest.fixture(scope="module")
def browser(request):
selenium = webdriver.Firefox()
selenium .implicitly_wait(3)
yield selenium
selenium.quit()
您知道为什么它不起作用吗?
然后在稍后我使用了另一个效果很好的代码
@pytest.fixture(scope="module")
def browser(request):
selenium = webdriver.Firefox()
selenium.implicitly_wait(3)
def teardown():
selenium.quit()
request.addfinalizer(teardown)
return selenium
不建议使用此方法:
此方法仍然得到完全支持,但建议从2.10开始产生收益,因为它被认为更简单,更好地描述了自然代码流。
关于版本的注释:
$ python -V
$ Python 3.5.2 :: Anaconda 4.2.0 (64-bit)
$ django-admin version
$ 1.10.3
$ pip show pytest
$ Name: pytest
$ Version: 2.9.2
根据文档:版本2.10之前,为了使用屈服语句执行拆除代码一个人必须使用farrevy_fixture标记标记固定装置。从2.10开始,普通固定装置可以直接使用屈服,因此不再需要屈服ixfixture装饰器,也可以被视为弃用。