简体   繁体   English

通过 pytest.main() 运行时以编程方式检索 pytest 结果

[英]retrieve pytest results programmatically when run via pytest.main()

I'd like to run pytest and then store results and present them to users on demand (eg store pytest results to a db and then expose them through web service) I could run pytest from a command line with option to save results report into file, then find and parse the file, but feels silly to have the results in a (pytest) python app, then store them to a file and then instantly look for the file, parse it back into python code for further processing.我想运行 pytest,然后存储结果并按需呈现给用户(例如,将 pytest 结果存储到数据库,然后通过 Web 服务公开它们)我可以从命令行运行 pytest,并选择将结果报告保存到文件中,然后找到并解析文件,但是在(pytest)python应用程序中获得结果感觉很愚蠢,然后将它们存储到文件中,然后立即查找文件,将其解析回python代码以进行进一步处理。 I know I can run pytest programatically via pytest.main(args) however it only return some exit code and not details about tests results - how can I retrieve the results when using pytest.main()?我知道我可以通过 pytest.main(args) 以编程方式运行 pytest,但是它只返回一些退出代码而不是有关测试结果的详细信息 - 使用 pytest.main() 时如何检索结果?

I'm looking for smt like我正在寻找类似的 smt

args =  # arguments 
ret_code = pytest.main(args=args) # pytest.main() as is only returns trivial return code
my_own_method_to_process(pytest.results)   # how to retrieve any kind of pytest.results object that would contain test execution results data (list of executed tests, pass fail info, etc as pytest is displaying into console or saves into file reports)

There are couple of similar questions but always with some deviation that doesn't work for me.有几个类似的问题,但总是有一些对我不起作用的偏差。 I simply want to run pytest from my code and - whatever format the output would be - directly grab it and further process.我只是想从我的代码中运行 pytest - 无论输出是什么格式 - 直接抓取它并进一步处理。 (Note I'm in a corporate environment where installing new packages (ie pytest plugins) is limited, so I'd like to achieve this without installing any other module/pytest plugin into my environment) (注意我在一个公司环境中安装新包(即pytest插件)是有限的,所以我想在不安装任何其他模块/pytest插件到我的环境中的情况下实现这一点)

Write a small plugin that collects and stores reports for each test.编写一个小插件来收集和存储每个测试的报告。 Example:例子:

import time
import pytest


class ResultsCollector:
    def __init__(self):
        self.reports = []
        self.collected = 0
        self.exitcode = 0
        self.passed = 0
        self.failed = 0
        self.xfailed = 0
        self.skipped = 0
        self.total_duration = 0

    @pytest.hookimpl(hookwrapper=True)
    def pytest_runtest_makereport(self, item, call):
        outcome = yield
        report = outcome.get_result()
        if report.when == 'call':
            self.reports.append(report)

    def pytest_collection_modifyitems(self, items):
        self.collected = len(items)

    def pytest_terminal_summary(self, terminalreporter, exitstatus):
        print(exitstatus, dir(exitstatus))
        self.exitcode = exitstatus.value
        self.passed = len(terminalreporter.stats.get('passed', []))
        self.failed = len(terminalreporter.stats.get('failed', []))
        self.xfailed = len(terminalreporter.stats.get('xfailed', []))
        self.skipped = len(terminalreporter.stats.get('skipped', []))

        self.total_duration = time.time() - terminalreporter._sessionstarttime

def run():
    collector = ResultsCollector()
    pytest.main(plugins=[collector])
    for report in collector.reports:
        print('id:', report.nodeid, 'outcome:', report.outcome)  # etc
    print('exit code:', collector.exitcode)
    print('passed:', collector.passed, 'failed:', collector.failed, 'xfailed:', collector.xfailed, 'skipped:', collector.skipped)
    print('total duration:', collector.total_duration)


if __name__ == '__main__':
    run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM