Python 如何在pytest中的teardown_module()中获得模块所有测试的结果(通过/失败)

Python 如何在pytest中的teardown_module()中获得模块所有测试的结果(通过/失败),python,pytest,Python,Pytest,这是我的设想。 我有一个test_1.py文件,它是这样的 import pytest def setup_module(): print 'setup module' def teardown_module(): print 'teardown module' class Test_ABC: def setup_class(cls): print 'setup class' def teardown_class(cls): pr

这是我的设想。 我有一个test_1.py文件,它是这样的

import pytest
def setup_module():
    print 'setup module'
def teardown_module():
    print 'teardown module'

class Test_ABC:
    def setup_class(cls):
        print 'setup class'
    def teardown_class(cls):
        print 'teardown class'
    def test_1(self):
        print 'test_1'
    def test_2(self):
        print 'test_2'
因此,我想知道是否有可能获得
test_1
test_2
测试的结果(通过/失败)。 比方说,如果测试套件中的任何测试失败,我需要发送一封电子邮件。
我正在调用测试包下conftest.py中的电子邮件模块,例如:

from _pytest.runner import runtestprotocol


def pytest_runtest_protocol(item, nextitem):

    reports = runtestprotocol(item, nextitem=nextitem)
    for report in reports:
        if report.when == 'call':
            print('\n%s --- %s' % (item.name, report.outcome))

                if report.outcome is 'passed':
                    print('cool too')
                elif report.outcome is 'failed':
                    print('cool')
    return True
另一个选择是:

    @pytest.fixture
    def after_test_report_pass_fail(request):
        yield
... your code goes here

然后,您可以在测试包下的conftest.py中报告每个项目(测试)

,例如:

from _pytest.runner import runtestprotocol


def pytest_runtest_protocol(item, nextitem):

    reports = runtestprotocol(item, nextitem=nextitem)
    for report in reports:
        if report.when == 'call':
            print('\n%s --- %s' % (item.name, report.outcome))

                if report.outcome is 'passed':
                    print('cool too')
                elif report.outcome is 'failed':
                    print('cool')
    return True
另一个选择是:

    @pytest.fixture
    def after_test_report_pass_fail(request):
        yield
... your code goes here

然后您可以报告每个项目(测试)

断言是确定测试通过/失败的商定形式,而不是返回。@DánielBencze我想要模块中所有测试的累积结果。通过返回true或false,我如何才能做到这一点?断言是确定测试通过/失败的商定形式,而不是返回。@DánielBencze我想要模块中所有测试的累积结果。我怎样才能通过返回真或假来做到这一点?