在鼻子测试中打印不同的长描述以及测试名称python

时间:2014-09-11 22:45:12

标签: python nose python-unittest

我正在使用命令:

nosetests test.py

运行此操作时,仅打印描述的第一行。 我想要整个描述以及测试名称。我该怎么做?

test.py文件

import unittests

class TestClass(unittest.TestCase):

    def test_1(self):
       """this is a long description //
              continues on second line which does not get printed """
       some code;
       self.assertTrue(True)

    def test_2(self):
       """this is another or different long description //
              continues on second line which does not get printed """
       some code;
       self.assertTrue(True)


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

1 个答案:

答案 0 :(得分:2)

单元测试是测试方法文档字符串的documented as only showing the first line。但您可以覆盖shortDescription方法的默认实现来自定义该行为:

import unittest

class TestClass(unittest.TestCase):

    def shortDescription(self):
        return self._testMethodDoc

    def test_1(self):
       """this is a long description //
              continues on second line """
       self.assertTrue(True)

    def test_2(self):
       """this is another or different long description //
              continues on second line which also gets printed :) """
       self.assertTrue(True)

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

演示:

$ nosetests -v example.py 
this is a long description //
              continues on second line ... ok
this is another or different long description //
              continues on second line which also gets printed :) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

有人写了一个鼻子插件来修复这个确切的烦恼,也许你有兴趣使用它。这是:https://bitbucket.org/natw/nose-description-fixer-plugin/