在setup()中输出pyunit测试名称的方法

时间:2010-12-21 22:39:54

标签: python unit-testing python-unittest

在python中是否有办法让pyunit测试输出当前正在运行的测试。例如:

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))

3 个答案:

答案 0 :(得分:71)

您可以使用self._testMethodName。这是inherited from the unittest.TestCase父类。

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

答案 1 :(得分:16)

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

您可以在以下位置找到文档: http://docs.python.org/library/unittest.html#unittest.TestCase.id

答案 2 :(得分:6)

您可以使用str(self.id()).split()[4]。可以在http://docs.python.org/library/unittest.html#unittest.TestCase.id

找到它
相关问题