Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python unittest,如何显示更好的组/测试名称?_Python_Python Unittest - Fatal编程技术网

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

使用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):
        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
,在根类中,我查找这个变量并在
设置类中显示它