如何在测试(或方法)中自动使用 pytest fixture?
How to use a pytest fixture in a test (or method) automatically?
根据我找到的信息 here,我尝试了以下操作:
@pytest.mark.usefixtures("driver")
class TestSuite(object):
def test1(self):
driver.log("Start the test")
但是我得到一个错误
NameError: global name 'driver' is not defined
所以信息不正确,还是我理解错了?
如何在TestSuite
class中的每个测试方法(或任何其他方法)中自动使用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
表示列出的灯具将被自动调用,而不是它们在您的代码中可供您调用。
我怀疑是否有一种方法可以在不明确命名的情况下使用夹具。
class TestSuite(object):
def test1(self, driver):
driver.log("Start the test")
是唯一的方法。
根据我找到的信息 here,我尝试了以下操作:
@pytest.mark.usefixtures("driver")
class TestSuite(object):
def test1(self):
driver.log("Start the test")
但是我得到一个错误
NameError: global name 'driver' is not defined
所以信息不正确,还是我理解错了?
如何在TestSuite
class中的每个测试方法(或任何其他方法)中自动使用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
表示列出的灯具将被自动调用,而不是它们在您的代码中可供您调用。
我怀疑是否有一种方法可以在不明确命名的情况下使用夹具。
class TestSuite(object):
def test1(self, driver):
driver.log("Start the test")
是唯一的方法。