Print a different long description in nose tests a

2019-05-11 23:42发布

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条回答
欢心
2楼-- · 2019-05-12 00:02

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/

查看更多
登录 后发表回答