2

I need to find the name of the test method about to be run from within the SetUp() method that unittest runs before each test. How can I do this without running every test method seperately?

Example:

class Testing(unittest2.TestCase):
    def setUp():
        # wish i could write:
        string = getNextTestMethodName()

    def test_example(self):
        self.assertNotEqual(0,1)
4

2 回答 2

1

You can use self.shortDescription() that will give you the name of the test (or the docstring associated with the test), and this, even in the setUp/tearDown methods.

EDIT: maybe self.id() is enough, it provide only the test name (thanks to @Blair).

于 2011-12-13T08:18:31.200 回答
0

not even self.id() is needed:

def setUp( self ):
    logger.info( '# setUp for %s' % ( self, ))

typical output:

# setUp for test_mainframe_has_been_constructed (__main__.BasicFunctionality_FT)

... where "test_mainframe_has_been_constructed" is the method.

So repr( self ), presumably, if you just want the string - then slice and dice, observing that the method name ends with the opening bracket (or first white space).

于 2015-10-03T18:26:44.220 回答