我正在使用 selenium 进行端到端测试,但我不知道如何使用setup_class
and teardown_class
方法。
我需要设置浏览器setup_class
方法,然后执行一系列定义为类方法的测试,最后退出浏览器teardown_class
method.
但从逻辑上讲,这似乎是一个糟糕的解决方案,因为事实上我的测试不适用于类,而是适用于对象。我通过self
每个测试方法中都有 param,这样我就可以访问对象的变量:
class TestClass:
def setup_class(cls):
pass
def test_buttons(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def test_buttons2(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def teardown_class(cls):
pass
甚至为类创建浏览器实例似乎也是不正确的。它应该为每个对象单独创建,对吧?
所以,我需要使用__init__
and __del__
方法而不是setup_class
and teardown_class
?
根据夹具完成/执行拆卸代码,当前设置和拆卸的最佳实践是使用yield
代替return
:
import pytest
@pytest.fixture()
def resource():
print("setup")
yield "resource"
print("teardown")
class TestResource:
def test_that_depends_on_resource(self, resource):
print("testing {}".format(resource))
运行它的结果是
$ py.test --capture=no pytest_yield.py
=== test session starts ===
platform darwin -- Python 2.7.10, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
collected 1 items
pytest_yield.py setup
testing resource
.teardown
=== 1 passed in 0.01 seconds ===
编写拆卸代码的另一种方法是接受request- 上下文对象进入你的固定功能并调用它request.addfinalizer
具有执行拆卸一次或多次的函数的方法:
import pytest
@pytest.fixture()
def resource(request):
print("setup")
def teardown():
print("teardown")
request.addfinalizer(teardown)
return "resource"
class TestResource:
def test_that_depends_on_resource(self, resource):
print("testing {}".format(resource))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)