Python unittest,如何显示更好的组/测试名称?
使用Python Unittest,下面是一个测试套件的示例:Python unittest,如何显示更好的组/测试名称?,python,python-unittest,Python,Python Unittest,使用Python Unittest,下面是一个测试套件的示例: import unittest # Here's our "unit". def IsOdd(n): return n % 2 == 1 # Here's our "unit tests". class IsOddTests(unittest.TestCase): def testOne(self): self.failUnless(IsOdd(1)) def testTwo(self
import unittest
# Here's our "unit".
def IsOdd(n):
return n % 2 == 1
# Here's our "unit tests".
class IsOddTests(unittest.TestCase):
def testOne(self):
self.failUnless(IsOdd(1))
def testTwo(self):
self.failIf(IsOdd(2))
def main():
unittest.main(verbosity=2)
if __name__ == '__main__':
main()
结果是:
testOne (__main__.IsOddTests) ... ok
testTwo (__main__.IsOddTests) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
是否可以增强测试的显示,例如:
Testing ODD method
Testing with value is 1 (__main__.IsOddTests) ... ok
Testing with value is 2 (__main__.IsOddTests) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
我想做的是,在大量测试的情况下,为每个测试用例显示一个组名(包含多个测试),并为每个测试显示一个名称(应该比函数名更明确)。要做到这一点,只需为测试设置一个docstring:
def testOne(self):
"""Test IsOdd(1)"""
self.failUnless(IsOdd(1))
def testTwo(self):
"""Test IsOdd(2)"""
self.failIf(IsOdd(2))
为您的测试选择docstring有一点技巧,稍后会有意义。不要害怕回去重构你的东西。命名我的测试太棒了,现在对于我的类,我也尝试了docstring,但没有任何运气。知道吗?我不相信
unittest.py
会在任何地方使用类中的docstring,至少在我检查过的版本中是这样。(在.py文件中查找文档)。我自己做了。我在每个类中添加了一个\uuuu display\uuuu
,在根类中,我查找这个变量并在设置类中显示它