Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Unittest shortDescription printing out None

I found out about the shortDescription function and was eager to try it out.

shortDescription() Returns a description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the test method’s docstring, if available, or None.

Strangely enough, I can't get it to work. Can anybody spot what I'm doing wrong?

My class does inherit from unittest.TestCase and even it has a docstring

def test_smth(self):
    """
    TEST
    """
    self.description = 'TEST!'
    print(self.shortDescription())

Prints out None in Python 3.6

like image 889
Nether Avatar asked Jun 24 '26 14:06

Nether


1 Answers

The first line of the doc-string is empty:

"""   <--- this is the first line
TEST
"""

By removing the first empty line, you will see what you want:

"""TEST
"""

➜  /tmp cat t.py
import unittest

class UT(unittest.TestCase):
    def test_smth(self):
        """TEST"""
        print('shortDescription():', self.shortDescription())


unittest.main()
➜  /tmp python3.6 t.py
shortDescription(): TEST
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

If you run tests with -v command line option, you can see the description printed instead of the test method name:

➜  /tmp python3.6 t.py -v
test_smth (__main__.UT)
TEST ... shortDescription(): TEST
ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
like image 138
falsetru Avatar answered Jun 26 '26 15:06

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!