Sam*_*dek 3 selenium pytest-django
我正在尝试使用pytest为我的Django项目运行selenium并执行fixture fix/teardown.
我试图遵循最佳实践使用,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()
Run Code Online (Sandbox Code Playgroud)
你知道它为什么不起作用吗?
然后,我使用了另一个运行良好的代码
@pytest.fixture(scope="module")
def browser(request):
selenium = webdriver.Firefox()
selenium.implicitly_wait(3)
def teardown():
selenium.quit()
request.addfinalizer(teardown)
return selenium
Run Code Online (Sandbox Code Playgroud)
但不建议使用此方法:
此方法仍然完全受支持,但建议从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
Run Code Online (Sandbox Code Playgroud)
根据文档:在版本2.10之前,为了使用yield语句执行拆卸代码,必须使用yield_fixture标记来标记一个fixture.从2.10开始,普通夹具可以直接使用yield,因此不再需要yield_fixture装饰器并且不推荐使用它.
| 归档时间: |
|
| 查看次数: |
2590 次 |
| 最近记录: |