A way to output pyunit test name in setup()

感情迁移 提交于 2019-11-27 13:26:02

问题


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

回答1:


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

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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4504622/a-way-to-output-pyunit-test-name-in-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!