A way to output pyunit test name in setup()

PythonUnit TestingPython Unittest

Python Problem Overview


Is there a way in python for a pyunit test to output the test it's currently running. Example:

def setUp(self):
    log.debug("Test %s Started" % (testname))

def test_example(self):
    #do stuff

def test_example2(self):
    #do other stuff

def tearDown(self):
    log.debug("Test %s Finished" % (testname))

Python Solutions


Solution 1 - Python

You can use self._testMethodName. This is inherited from the unittest.TestCase parent class.

def setUp():
    print "In method", self._testMethodName

Solution 2 - Python

Solution 3 - Python

You can usestr(self.id()).split()[4]. It could be found here http://docs.python.org/library/unittest.html#unittest.TestCase.id

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian O'NeillView Question on Stackoverflow
Solution 1 - PythonTyler HobbsView Answer on Stackoverflow
Solution 2 - PythonYoung-hwiView Answer on Stackoverflow
Solution 3 - PythonXiaoView Answer on Stackoverflow