python 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.assertNotEqual(0,1)

最佳答案

您可以使用 self.shortDescription()这将为您提供测试的名称(或与测试关联的文档字符串),甚至在 setUp/tearDown 方法中也是如此。

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

关于python unittest2 - 将测试方法名称暴露给设置方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8486087/

相关文章:

python - 我如何从两个嵌套列表(父级和子级)中创建目录列表

matplotlib - 更改 matplotlib 极坐标图中的标签

python - 是否可以使用 unit2 运行 doctests

python - pandas 中的关键错误

python - 将 JSON 时间戳字符串转换为 pandas dataframe 中的 python 日期

python - 损坏的图像 - Google App Engine

python - fd 从 python 到子进程的重复

python - 适用于带有 apache 2 和 Python 2.6 的 Windows 的 mod_wsgi

来自脚本的 python Nose ,从文件中收集测试类,然后运行测试

Django 单元测试 - 配置不当错误