Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to output pyunit test name in setup()

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))
like image 667
Brian O'Neill Avatar asked Dec 21 '10 22:12

Brian O'Neill


People also ask

What does Unittest main () do?

Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.

What is setup method in Python?

You can read more with examples here. When a setUp() method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test.


3 Answers

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

def setUp():
    print("In method", self._testMethodName)
like image 124
Tyler Hobbs Avatar answered Sep 27 '22 12:09

Tyler Hobbs


self.id().split('.')[-1]

You can find the Documentation at: http://docs.python.org/library/unittest.html#unittest.TestCase.id

edit: For 2.7 users, https://docs.python.org/2.7/library/unittest.html#unittest.TestCase.id

like image 26
Young-hwi Avatar answered Sep 27 '22 12:09

Young-hwi


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

like image 29
Xiao Avatar answered Sep 27 '22 12:09

Xiao