Unittest的subTest不适用于Pytest,还是我疯了?

问题描述 投票:0回答:1

据我了解,如果测试失败,pytest应该提供带有unittest.TestCase.subtest()的参数信息。所以这是类似于我的代码的东西:

class TestStuff(unittest.TestCase):

    def test_foo(self):
        for i in range(0, 100):
            with self.subTest(msg = "seed", i = i):
                np.random.seed(i)
                n = np.random.randint(0, 30)
                self.assertGreaterEqual(28, n)

这当然会失败,并打印出以下内容:

================================================================================================== test session starts ===================================================================================================
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/foopackage
plugins: openfiles-0.3.2, arraydiff-0.3, doctestplus-0.3.0, remotedata-0.3.1
collected 7 items                                                                                                                                                                                                        

foo.py ......F                                                                                                                                                                                               [100%]

======================================================================================================== FAILURES ========================================================================================================
________________________________________________________________________________________________ TestStuff.test_foo _________________________________________________________________________________________________

self = <foo.test_foo.TestStuff testMethod=test_foo>

    def test_foo(self):
        for i in range(0, 100):
            with self.subTest(msg = "seed", i = i):
                np.random.seed(i)
                n = np.random.randint(0, 30)
>               self.assertGreaterEqual(28, n)
E               AssertionError: 28 not greater than or equal to 29

foo.py:135: AssertionError
=========================================================================================== 1 failed, 6 passed in 1.91 seconds ===========================================================================================

如您所见,没有消息说明哪个种子(i的值)未通过测试。我到处都读到Pytest与unittest兼容,所以我似乎看不到这里的问题。有人可以解释吗? THX

python testing pytest python-unittest
1个回答
0
投票

需要添加pytest-subtest插件才能添加该功能

© www.soinside.com 2019 - 2024. All rights reserved.