A way to output pyunit test name in setup()
Asked Answered
R

3

55

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))
Reniti answered 21/12, 2010 at 22:39 Comment(0)
T
101

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

def setUp():
    print("In method", self._testMethodName)
Trochelminth answered 22/12, 2010 at 5:1 Comment(2)
Where's the documentation for this?Subtreasury
There's not any. As you might guess from the underscore prefix on the attribute name, this is an internal attribute that's not intended to be exposed. I do wish that there was a proper way to do this, though.Trochelminth
H
19
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

Harberd answered 19/2, 2013 at 9:45 Comment(0)
W
6

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

Wellesley answered 4/7, 2011 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.