Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pytest.hookimpl将pytest函数返回值写入文件_Python_Return_Pytest_Return Value_Benchmarking - Fatal编程技术网

Python 使用pytest.hookimpl将pytest函数返回值写入文件

Python 使用pytest.hookimpl将pytest函数返回值写入文件,python,return,pytest,return-value,benchmarking,Python,Return,Pytest,Return Value,Benchmarking,我正在寻找一种访问测试函数返回值的方法,以便将该值包含在测试报告文件中(类似于) 我想使用的代码示例: # modified example code from http://doc.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures import pytest import os.path @pytest.hookimpl(tryfirst=True, hookwrapper=True) d

我正在寻找一种访问测试函数返回值的方法,以便将该值包含在测试报告文件中(类似于)

我想使用的代码示例:

# modified example code from http://doc.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures

import pytest
import os.path

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.passed:
        mode = "a" if os.path.exists("return_values") else "w"
        with open("return_values.txt", mode) as f:
            # THE FOLLOWING LINE IS THE ONE I CANNOT FIGURE OUT
            # HOW DO I ACCESS THE TEST FUNCTION RETURN VALUE?
            return_value = item.return_value
            f.write(rep.nodeid + ' returned ' + str(return_value) + "\n")
我希望返回值被写入文件“return_values.txt”。相反,我得到了一个AttributeError

背景(如果您可以推荐完全不同的方法):

# modified example code from http://doc.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures

import pytest
import os.path

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.passed:
        mode = "a" if os.path.exists("return_values") else "w"
        with open("return_values.txt", mode) as f:
            # THE FOLLOWING LINE IS THE ONE I CANNOT FIGURE OUT
            # HOW DO I ACCESS THE TEST FUNCTION RETURN VALUE?
            return_value = item.return_value
            f.write(rep.nodeid + ' returned ' + str(return_value) + "\n")
我有一个Python库,用于对给定问题进行数据分析。我有一套标准的测试数据,我会定期运行我的分析,以生成关于分析算法质量的各种“基准”指标。例如,一个这样的度量是由分析代码生成的规范化混淆矩阵的跟踪(我希望它尽可能接近1)。另一个指标是产生分析结果的CPU时间

我正在寻找一种很好的方法,将这些基准测试结果包含到CI框架(目前是Jenkins)中,这样就很容易看到提交是提高还是降低了分析性能。由于我已经在CI序列中运行pytest,并且我想将pytest的各种功能用于我的基准测试(fixture、marks、skipping、cleanup),所以我考虑在pytest中添加一个后处理钩子(请参阅),该钩子收集测试函数运行时和返回值并报告它们(或仅标记为基准测试的测试项目)放入一个文件中,该文件将作为测试工件由我的CI框架收集和归档


我对解决这个问题的其他方法持开放态度,但我的谷歌搜索结论是pytest是最接近我所需要的框架。

pytest
忽略测试函数返回值,如图所示:

但是,您可以在test函数中存储所需的任何内容;我通常使用
config
对象进行存储

import pathlib
import pytest


def pytest_configure(config):
    # create the dict to store custom data
    config._test_results = dict()


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.passed:
        # get the custom data
        return_value = item.config._test_results.get(item.nodeid, None)
        # write to file
        report = pathlib.Path('return_values.txt')
        with report.open('a') as f:
            f.write(rep.nodeid + ' returned ' + str(return_value) + "\n")
现在将数据存储在测试中:

def test_fizz(request):
    request.config._test_results[request.node.nodeid] = 'mydata'

同样的问题,我提出了一个不同的解决方案:

在测试中使用夹具
记录\u属性

def test_mytest(record_property):
    record_property("key", 42)
然后在
conftest.py
中,我们可以使用
pytest\u runtest\u teardown

然后是
test\u mytest\u return\u values.txt的内容

key = 42
两个重要注意事项:
  • 即使测试失败,也将执行此代码。我找不到获得测试结果的方法
  • 这可以与的答案结合使用
    results=dict(item.user\u properties)
    来获得在测试中添加的键和值,而不是将dict添加到配置中,然后在测试中访问它

  • 测试函数没有返回值,因此
    pytest
    会忽略它们。不过,您可以将自定义数据存储在测试函数本身中,例如,在
    request.config
    :smth like
    request.config.\u test\u results[request.node.nodeid]=mydata
    ,然后通过
    item.config.\u test\u results[item.nodeid]访问它
    在hookimpl中。谢谢!我会接受这个答案。谢谢!这对我来说并不明显,所以仅供参考-您需要将第二段代码放在conftest.py文件中。我无法使用@hoefling的
    config
    方法,但这很有效,而且读起来很自然
    key = 42