54

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))
Brian O'Neill
  • 670
  • 1
  • 5
  • 6

3 Answers3

99

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

def setUp():
    print("In method", self._testMethodName)
RiveN
  • 2,595
  • 11
  • 13
  • 26
Tyler Hobbs
  • 6,872
  • 24
  • 31
  • 3
    Where's the documentation for this? – Cory May 12 '11 at 13:13
  • 8
    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. – Tyler Hobbs May 13 '11 at 21:00
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

Young-hwi
  • 607
  • 7
  • 14
6

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

guettli
  • 25,042
  • 81
  • 346
  • 663
Xiao
  • 12,235
  • 2
  • 29
  • 36