如何将单元测试子测试转换为 pytest

mes*_*shy 7 python pytest python-3.x python-unittest

pytest(2.8.3)是否有等价物self.subTest()(如在 Python 3 中找到的unittest)?

这是我尝试转换的代码的简化版本:

class MyUnittestTest(TestCase):
    def test_permutations(self):
        on_off = True, False
        for prefix, params, suffix in product(on_off, on_off, on_off):

            expected_prefix = 'something' if prefix else ''
            expected_params = ('something',) if params else ()
            expected_suffix = b'something' if suffix else b''

            with self.subTest(prefix=prefix, params=params, suffix=suffix):
                result = do_magic(prefix=prefix, params=params, suffix=suffix)

                self.assertEqual(result.prefix, expected_prefix)
                self.assertEqual(result.params, expected_params)
                self.assertEqual(result.suffix, expected_suffix)
Run Code Online (Sandbox Code Playgroud)

目前,我所拥有的只是为每个排列定义一个测试。一定有比这更好的方法:

class MyPytestTest:
    def test_on_on_on(self):
        expected_prefix = ...

        result = do_magic(...)

        assert ...

    def test_on_on_off(self):
        ...
Run Code Online (Sandbox Code Playgroud)

mes*_*shy 4

Pytest 本身没有此功能,但可以通过pytest-subtests插件添加。

要使用它,请将subtests夹具注入测试中:

def test_permutations(subtests):
    on_off = True, False
    for prefix, params, suffix in product(on_off, on_off, on_off):
        with subtests.test(prefix=prefix, params=params, suffix=suffix):
            result = do_magic(prefix=prefix, params=params, suffix=suffix)
            assert ...
Run Code Online (Sandbox Code Playgroud)

在某些情况下pytest.mark.parametrize可能是更好的选择,尽管它的工作方式不同。