2010-12-21 27 views
35

Pyunit測試是否有一種方法輸出當前正在運行的測試。例如:在setup()中輸出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)) 

回答

51

您可以使用self._testMethodName。這是從unittest.TestCase父類繼承而來的。

def setUp(): 
    print "In method", self._testMethodName 
0
>>> class A: 
    pass 
>>> a = A() 
>>> a 
<__main__.A instance at 0x203cf80> 
>>> a.__class__.__name__ 
'A' 
>>> 
+1

這是類名,而不是方法的名稱,再加上他試圖把它放在`setUp`和`tearDown`中,而不是在方法本身中。 – 2010-12-21 23:09:52

+0

正確。需要setUp和tearDown中的方法名稱。謝謝丹尼爾 – 2010-12-21 23:14:49

相關問題