有没有办法在详细模式下在pycharm中运行unittests。我正在寻找一种方法来查看测试函数中的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
答案 0 :(得分:2)
您所要做的就是使用setUp
方法并调用_testMethodDoc
属性,如下所示:
def setUp(self):
print(self._testMethodDoc)
您可以为继承自unittest.TestCase
的unittest创建自己的基类,但如果您想稍后覆盖setUp
方法,则必须调用super
。这是一个缩短代码实现的选项:
class BaseUnitTest(unittest.TestCase):
def setUp(self):
print(self._testMethodDoc)
class KnownValues(BaseUnitTest):
...
答案 1 :(得分:0)
试试这个
if __name__ == '__main__':
unittest.main(verbosity=2)