根据我找到的信息here ,我尝试了以下方法:
@pytest.mark.usefixtures("driver")
class TestSuite(object):
def test1(self):
driver.log("Start the test")
但我收到错误
NameError: global name 'driver' is not defined
所以信息不正确,还是我误解了?
如何在 TestSuite
类内的每个测试方法(或任何其他方法)中自动使用固定装置?
更多信息:
- 我需要为每个测试重新初始化 fixture
通常一次测试可能需要 5 个灯具。但由于我在每个测试中使用一些方法,所以我需要像这样将它们全部传递
def do_something(self, fixture1, fixture2, fixture3, fixture4, fixture5): ... def test1(self, fixture1, fixture2, fixture3, fixture4, fixture5): do_something(fixture1, fixture2, fixture3, fixture4, fixture5)
也许情况不会那么糟糕,但如果我可以在每次测试时自动使用这些装置,那将会有所帮助......
最佳答案
usefixtures
表示列出的装置将被自动调用,而不是它们将在您的代码中可供您调用。
我怀疑是否有一种方法可以在不显式命名的情况下使用 fixture 。
class TestSuite(object):
def test1(self, driver):
driver.log("Start the test")
这是唯一的方法。
关于python - 如何在测试(或方法)中自动使用 pytest fixture ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48536645/