python – 在详细模式下运行unittest的Pycharm

有没有办法在详细模式下运行pycharm中的单元测试.我正在寻找一种方法来查看测试函数中的docstring,以便我可以看到run test的一些信息.

class KnownValues(unittest.TestCase):
    known_values = (
        ([1, 2],[[1, 2]]),
        ([1, 2, 3], [[1, 2], [1, 2, 3]]),
        ([1, 2, 3, 4],[[1, 2], [1, 2, 3, 4]]),
        ([1, 2, 3, 4, 5],[[1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]),
        ([1, 2, 3, 4, 5, 6],[[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6]]),
              )

    def test_check(self):
        '''This should check is the function returning right'''
        for arg, result in self.known_values:
            print("Testing arg:{}".format(arg))
            assert program.lister(arg) == result


if __name__ == '__main__':
    unittest.main()

它返回:

Testing started at 19:38 ч. ...
Testing arg:[1, 2]

Process finished with exit code 0

我想得到:

test_check (__main__.KnownValues)
This should check is the function returning right ... Testing arg:[1, 2]
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

最佳答案 您所要做的就是使用setUp方法并像这样调用_testMethodDoc属性:

def setUp(self):
    print(self._testMethodDoc)

您可以为您的unittest创建自己的基类,继承自unittest.TestCase)但是如果您想稍后重写setUp方法,则必须调用super.这是一个更短的代码实现选项:

class BaseUnitTest(unittest.TestCase):
    def setUp(self):
        print(self._testMethodDoc)


class KnownValues(BaseUnitTest):
    ...
点赞