Python 使用pytest从单个函数报告多个测试

Python 使用pytest从单个函数报告多个测试,python,pytest,Python,Pytest,我目前有一个简单的测试,它实例化了一堆类似的对象,并执行了一个方法,以确保该方法不会引发任何异常: class TestTemplates(object): def test_generate_all(self): '''Generate all the templates and ensure none of them throw validation errors''' for entry_point in pkg_resources.iter_en

我目前有一个简单的测试,它实例化了一堆类似的对象,并执行了一个方法,以确保该方法不会引发任何异常:

class TestTemplates(object):

    def test_generate_all(self):
        '''Generate all the templates and ensure none of them throw validation errors'''
        for entry_point in pkg_resources.iter_entry_points('cloudformation.template'):
            object = entry_point.load()
            object().build().to_json()
这在
pytest
的文本输出中作为单个测试报告:

test/test_templates.py::TestTemplates::test_generate_all PASSED
同样在
junit
XML中:

<testcase classname="test.test_templates.TestTemplates" file="test/test_templates.py" line="31" name="test_generate_all" time="0.0983951091766"></testcase>


是否可以在不为每个对象手动定义测试功能的情况下,将每个测试对象作为单独的测试进行报告?

我会将您的对象列表定义为夹具,然后将该列表传递给参数化测试:

@pytest.fixture
def entry_point_objects()
    eps = pkg_resources.iter_entry_points('cloudformation.template')
    return [ep.load() for ep in eps]

@pytest.mark.parametrize('obj', entry_point_objects())
def test_generate_all(obj):
    obj().build().to_json()  

我会将该列表生成为一个fixture,然后将该fixture传递到一个参数化测试中。这正好给出了我想要的结果,谢谢。它与
fixture
一起工作吗?我尝试了这一点,但只有当
entry\u point\u objects
被声明为普通Python时才起作用function@CHINTANVADGAMA:(以及其他感兴趣的人)在Pytest 4.0中已经删除了直接调用fixture(如此处),因此不,向上投票的解决方案将不起作用。正如您所说,
entry\u point\u objects
应该是一个常规函数-文档中也建议这样做(参见示例:)。