Print a different long description in nose tests along with test name python

纵然是瞬间 提交于 2020-01-02 08:50:28

问题


I am using the command:

nosetests test.py

When this is run only the first line of the description gets printed. I want the whole description along with the test name. How do i do that?

test.py file

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:


Unittest is documented as only showing the first line of the test method's docstring. But you could override the default implementation of shortDescription method to customise that behaviour:

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)

Demo:

$ 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

Someone wrote a nose plugin to fix this exact annoyance, maybe you'd be interested to use that. Here it is: https://bitbucket.org/natw/nose-description-fixer-plugin/



来源:https://stackoverflow.com/questions/25798139/print-a-different-long-description-in-nose-tests-along-with-test-name-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!