Nether
Nether

Reputation: 1160

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

Upvotes: 2

Views: 3506

Answers (2)

Jay Parikh
Jay Parikh

Reputation: 2489

You are just missing self in your function argument. Rest of the things are good to go. Try it out.

Upvotes: 1

falsetru
falsetru

Reputation: 369364

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

Upvotes: 10

Related Questions