相关疑难解决方法(0)

你如何在python中生成动态(参数化)单元测试?

我有一些测试数据,想为每个项目创建一个单元测试.我的第一个想法是这样做:

import unittest

l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]]

class TestSequence(unittest.TestCase):
    def testsample(self):
        for name, a,b in l:
            print "test", name
            self.assertEqual(a,b)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

这样做的缺点是它在一次测试中处理所有数据.我想在运行中为每个项目生成一个测试.有什么建议?

python unit-testing parameterized-unit-test

211
推荐指数
10
解决办法
8万
查看次数

使用 pytest 参数化类测试

我有一组对象,我需要为我的测试类中的每个测试运行这些对象。我想参数化 TestClass 中的每个测试函数。最终目标是有类似的东西:

@pytest.mark.parametrize('test_input', [1, 2, 3, 4])
class TestClass:
    def test_something1(self, test_input):
        # test code here, runs each time for the parametrize
Run Code Online (Sandbox Code Playgroud)

但是根据我的理解,您不能传递输入参数,或者至少不能调用@pytest.mark.parametrize一个类,这些标记用于defsnot classs

我现在所拥有的:

class TestClass:
    def test_something1(self):
        for i in stuff:
            # test code here

    def test_something2(self):
        for i in stuff:
            # test code here
    ...
Run Code Online (Sandbox Code Playgroud)

有没有办法传递参数化类本身或 TestClass 中的每个函数?也许一个@pytest.mark.parametrize里面@pytest.fixture...(autouse=True).

我想将我的测试组织成一个类,因为它反映了我正在测试的文件。因为我在至少十几个不同的测试中循环这些对象,所以调用类的循环比在每个def.

python pytest python-3.x

4
推荐指数
2
解决办法
9767
查看次数