Python pytest中的嵌套循环

Python pytest中的嵌套循环,python,python-3.x,unit-testing,automated-tests,pytest,Python,Python 3.x,Unit Testing,Automated Tests,Pytest,正如我们现在看到的,为了迭代一个简单的测试,我们可以使用下面的脚本 @pytest.mark.parametrize("b", ["b1", "b2", "b3"]) def test_3(b): pass 输出将是 test_1.py::test_3[b1] PASSED test_1.py::test_3[b2] PASSED test_1.py::test_3[b3] PASSED 然后,如果我想作

正如我们现在看到的,为了迭代一个简单的测试,我们可以使用下面的脚本

@pytest.mark.parametrize("b", ["b1", "b2", "b3"])
def test_3(b):
    pass
输出将是

test_1.py::test_3[b1] PASSED
test_1.py::test_3[b2] PASSED
test_1.py::test_3[b3] PASSED
然后,如果我想作为一个场景循环通过多个测试,我会进行类参数化:

@pytest.mark.parametrize("a", ['a1', 'a2', 'a3'], scope="class")
class TestAB:

    def test_1(self, a):
        pass

    def test_2(self, a):
        pass

    def test_3(self, a):
        pass
我们得到

test_1.py::TestAB::test_1[a1] PASSED
test_1.py::TestAB::test_2[a1] PASSED
test_1.py::TestAB::test_3[a1] PASSED
test_1.py::TestAB::test_1[a2] PASSED
test_1.py::TestAB::test_2[a2] PASSED
test_1.py::TestAB::test_3[a2] PASSED
test_1.py::TestAB::test_1[a3] PASSED
test_1.py::TestAB::test_2[a3] PASSED
test_1.py::TestAB::test_3[a3] PASSED
这两个脚本产生预期的输出

问题: 为了将这两者结合起来并在类循环中循环测试,我应该写些什么,这样我就得到了这个输出

test_1.py::TestAB::test_1[a1] PASSED
test_1.py::TestAB::test_2[a1] PASSED
test_1.py::TestAB::test_3[b1-a1] PASSED
test_1.py::TestAB::test_3[b2-a1] PASSED
test_1.py::TestAB::test_3[b3-a1] PASSED

test_1.py::TestAB::test_1[a2] PASSED
test_1.py::TestAB::test_2[a2] PASSED
test_1.py::TestAB::test_3[b1-a2] PASSED
test_1.py::TestAB::test_3[b2-a2] PASSED
test_1.py::TestAB::test_3[b3-a2] PASSED

test_1.py::TestAB::test_1[a3] PASSED
test_1.py::TestAB::test_2[a3] PASSED
test_1.py::TestAB::test_3[b1-a3] PASSED
test_1.py::TestAB::test_3[b2-a3] PASSED
test_1.py::TestAB::test_3[b3-a3] PASSED
我试过但没用的东西
  • 从逻辑上讲,我是这样写的
  • 但这产生了

    test_1.py::TestAB::test_1[a1] PASSED
    test_1.py::TestAB::test_2[a1] PASSED
    test_1.py::TestAB::test_3[b1-a1] PASSED
    test_1.py::TestAB::test_1[a2] PASSED
    test_1.py::TestAB::test_2[a2] PASSED
    test_1.py::TestAB::test_3[b1-a2] PASSED
    test_1.py::TestAB::test_1[a3] PASSED
    test_1.py::TestAB::test_2[a3] PASSED
    test_1.py::TestAB::test_3[b1-a3] PASSED
    test_1.py::TestAB::test_3[b2-a1] PASSED
    test_1.py::TestAB::test_3[b2-a2] PASSED
    test_1.py::TestAB::test_3[b2-a3] PASSED
    test_1.py::TestAB::test_3[b3-a1] PASSED
    test_1.py::TestAB::test_3[b3-a2] PASSED
    test_1.py::TestAB::test_3[b3-a3] PASSED
    
  • 我还使用和来定义顺序。但它们甚至打破了类迭代本身的初始预期顺序

  • 那么,除了顺序之外,您对当前输出感到满意吗?为什么测试的顺序很重要?它们不应该独立于一个吗another@oli是的,但是因为它是一个测试场景,所以它有
    0
    值,而不保留order@gold_cy如果您使用
    pytest
    进行单元测试-是的,可能有独立的测试是一个优势。我正在做的是基于场景的测试,这可能需要发送多个API请求,并在第二个请求中使用第一个响应的数据,例如pytest文档中的数据-
    pytest
    主要用于单元测试。从技术上讲,您的测试不应该触及实际的API端点,因为这违背了单元测试的概念。
    test_1.py::TestAB::test_1[a1] PASSED
    test_1.py::TestAB::test_2[a1] PASSED
    test_1.py::TestAB::test_3[b1-a1] PASSED
    test_1.py::TestAB::test_1[a2] PASSED
    test_1.py::TestAB::test_2[a2] PASSED
    test_1.py::TestAB::test_3[b1-a2] PASSED
    test_1.py::TestAB::test_1[a3] PASSED
    test_1.py::TestAB::test_2[a3] PASSED
    test_1.py::TestAB::test_3[b1-a3] PASSED
    test_1.py::TestAB::test_3[b2-a1] PASSED
    test_1.py::TestAB::test_3[b2-a2] PASSED
    test_1.py::TestAB::test_3[b2-a3] PASSED
    test_1.py::TestAB::test_3[b3-a1] PASSED
    test_1.py::TestAB::test_3[b3-a2] PASSED
    test_1.py::TestAB::test_3[b3-a3] PASSED