获取Nos发现的所有测试的数据结构

2024-04-28 03:40:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如何获得包含Nose发现的所有测试列表的某种数据结构?我遇到了这个:

List all Tests Found by Nosetest

我正在寻找一种方法来获取我自己的python脚本中的单元测试名称列表(以及位置或虚线位置)。你知道吗


Tags: 方法脚本名称数据结构列表bytests单元测试
1条回答
网友
1楼 · 发布于 2024-04-28 03:40:40

有几种方法可以实现这一点:一种是使用带有 collect-only的xunit插件运行nose并解析生成的junitxml文件。或者,您可以添加一个捕获测试名称的基本插件,如下所示:

import sys
from unittest import TestCase

import nose
from nose.tools import set_trace


class CollectPlugin(object):
    enabled = True
    name = "test-collector"
    score = 100


    def options(self, parser, env):
        pass

    def configure(self, options, conf):
        self.tests = []

    def startTest(self, test):
        self.tests.append(test)

class MyTestCase(TestCase):
    def test_long_integration(self):
        pass
    def test_end_to_end_something(self):
        pass



if __name__ == '__main__':
    # this code will run just this file, change module_name to something 
    # else to make it work for folder structure, etc

    module_name = sys.modules[__name__].__file__

    plugin = CollectPlugin()

    result = nose.run(argv=[sys.argv[0],
                            module_name,
                            ' collect-only',
                            ],
                      addplugins=[plugin],)

    for test in plugin.tests:
        print test.id()

您的测试信息都捕获在plugin.test结构中。你知道吗

相关问题 更多 >