python - 如何在测试(或方法)中自动使用 pytest fixture ?

标签 python pytest

根据我找到的信息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/

相关文章:

python - 导入时没有名为 'PyPDF2' 的模块

python - 尽管超时,Pytest 仍然挂起

python - 编写一个 pytest 函数来检查输出到 python 中的文件?

python - 在 WAF 脚本中处理更改的文件

python - 如何在 Linux 中捕获按键(按键记录)?

Python线在同一个图上绘制多个时间序列

python - 在 matplotlib 中的嵌套字典的数据上创建图形(绘图)

python - 当 py.test 测试失败时,PyCharm 可以进入调试吗

python - pytest 模拟 os.listdir 返回空列表

python - 回滚事务不适用于 py.test 和 Flask