Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python unittest2-向安装方法公开测试方法名称_Python_Python 2.6_Unittest2 - Fatal编程技术网

python unittest2-向安装方法公开测试方法名称

python unittest2-向安装方法公开测试方法名称,python,python-2.6,unittest2,Python,Python 2.6,Unittest2,我需要从unittest在每次测试之前运行的SetUp()方法中找到要运行的测试方法的名称。在不单独运行每个测试方法的情况下,如何做到这一点 例如: class Testing(unittest2.TestCase): def setUp(): # wish i could write: string = getNextTestMethodName() def test_example(self): self.assertNotE

我需要从unittest在每次测试之前运行的
SetUp()
方法中找到要运行的测试方法的名称。在不单独运行每个测试方法的情况下,如何做到这一点

例如:

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

    def test_example(self):
        self.assertNotEqual(0,1)
您可以使用它来提供测试的名称(或与测试关联的文档字符串),甚至在
setUp/tearDown
方法中也可以使用它

编辑:也许足够了,它只提供测试名称(感谢@Blair)。

甚至不需要self.id()

def setUp( self ):
    logger.info( '# setUp for %s' % ( self, ))
典型输出:

# setUp for test_mainframe_has_been_constructed (__main__.BasicFunctionality_FT)
。。。其中“已构建测试主机”是方法


因此,
repr(self)
,假设您只需要字符串,然后切片,观察方法名称以开始括号(或第一个空格)结尾。

或者,返回测试用例的名称,通常是测试方法的名称。感谢cedric&blair,在查看unittest.py之后,我意识到testcase包含当前在self中运行的testmethod的名称。这满足了我的需要,因为id()不是很好,因为我的testcase类中有几个testmethods