50

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

3 回答 3

88

您可以使用self._testMethodName. 这是从父类继承的。unittest.TestCase

def setUp():
    print "In method", self._testMethodName
于 2010-12-22T05:01:01.277 回答
18
self.id().split('.')[-1]

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

编辑:对于 2.7 用户, https ://docs.python.org/2.7/library/unittest.html#unittest.TestCase.id

于 2013-02-19T09:45:56.353 回答
6

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

于 2011-07-04T10:46:28.147 回答