在 pytest 框架中的另一个测试用例中调用测试用例

MaY*_*HEM 5 pytest python-2.7 pytest-django

我正在使用 pytest 进行自动化。

pytest 中是否有任何选项可以在另一个测试用例中调用测试用例。

Fra*_*k T -1

你当然可以。对于 pytest,测试用例是一个简单的函数或方法。

例如:

def test_foo():
    assert 2 + 2 == 4

def test_bar():
    assert [1] + [2] == [1, 2]
    test_foo()
Run Code Online (Sandbox Code Playgroud)

考虑一下您为什么要这样做。通常,保持测试解耦会带来好处,例如在测试失败时更容易进行调试。

  • 您的示例不具有代表性,因为测试用例的签名可能包括固定装置和其他参数。 (9认同)