无法在装饰器中捕获 pytest 的结果

2024-03-16

我的 pytest 测试装饰器在调用函数后立即退出装饰器。如果我使用 python 而不是 pytest 运行该文件,效果会很好。

这是代码:

def dec(func):
    def wrapper(*args, **kwargs):
        print('do some stuff')
        result = func(*args, **kwargs)
        print('ran function')
        return False if result else return True
    return wrapper

@dec
def test_something_is_not_true():
    return False # assert False

@dec
def test_something_is_true():
    return True # assert True


print(test_something_is_not_true())
print(test_something_is_true())

当我用 python 运行该文件时,结果如下:

C:\tests> python test.py
do some stuff
ran function
True
do some stuff
ran function
False

效果很好!

但是当我用 pytest 运行它时,它停在result = func(*args, **kwargs)行并且从不执​​行print('ran function') line:

C:\tests>pytest test.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: C:\, inifile:
plugins: metadata-1.7.0, jira-0.3.6, html-1.19.0
collected 2 items

test.py F.                                                               [100%]

================================== FAILURES ===================================
_________________________ test_something_is_not_true __________________________

args = (), kwargs = {}

    def wrapper(*args, **kwargs):
        print('do some stuff')
>       result = func(*args, **kwargs)

test.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    @dec
    def test_something_is_not_true():
>       assert False
E       assert False

test.py:31: AssertionError
---------------------------- Captured stdout call -----------------------------
do some stuff
===================== 1 failed, 1 passed in 0.11 seconds ======================

正如您所看到的,装饰器函数实际上并没有做任何事情...但是如果我能让它工作,那么我可以查看测试是否通过了该函数,如果是,则根据通过的结果执行一些额外的逻辑或测试失败。也许使用装饰器来完成捕获测试的输出并不是最好的方法?

你会怎么办?


如果您需要在测试之前和之后执行代码,可以在fixture https://docs.pytest.org/en/latest/fixture.html。例子:

import pytest

@pytest.fixture
def wrapper(request):
    print('\nhere I am before the test')
    print('test function name is', request.node.name)
    print('test file is located under', request.node.fspath)
    yield
    print('\nand here I am after the test')


@pytest.mark.usefixtures('wrapper')
def test_spam_good():
    assert True

@pytest.mark.usefixtures('wrapper')
def test_spam_bad():
    assert False

正如您所看到的,这比编写自定义装饰器要强大得多。作为pytest非常擅长围绕测试进行元编程,您可能需要的很多东西已经存在,您只需要知道如何访问它即可。pytest docs https://docs.pytest.org/en/latest/reference.html包含许多适合初学者的示例和食谱。


如果您需要夹具中的测试结果,文档中有一个方法:在夹具中提供测试结果信息 https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures。创建文件conftest.py在项目根目录中包含以下内容:

import pytest


@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()

    # set a report attribute for each phase of a call, which can
    # be "setup", "call", "teardown"

    setattr(item, "rep_" + rep.when, rep)

现在您可以通过以下方式访问测试结果request自定义装置中的装置:

@pytest.fixture
def something(request):
    yield
    # request.node is an "item" because we use the default
    # "function" scope
    if request.node.rep_setup.failed:
        print("setting up a test failed!", request.node.nodeid)
    elif request.node.rep_setup.passed:
        if request.node.rep_call.failed:
            print("executing test failed", request.node.nodeid)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在装饰器中捕获 pytest 的结果 的相关文章

  • 为什么Python有最大递归深度?

    Python有最大递归深度 但没有最大迭代深度 为什么递归受到限制 把递归当成迭代来对待 而不限制递归调用的次数不是更自然吗 我只想说这个问题的根源来自于尝试实现流 参见这个问题 https stackoverflow com questi
  • 带有指针数组的 cython

    我在 python 中有一个 numpy ndarrays 列表 具有不同的长度 并且需要非常快速地访问 python 中的列表 我认为指针数组就可以解决问题 我试过 float type t list of arrays no of ar
  • Pip install 导致此错误“ cl.exe' failed with exit code 2 ”

    我已经阅读了有关此错误的所有其他问题 但令人沮丧的是 没有一个给出有效的解决方案 如果我跑pip install sentencepiece在命令行中 它给出了以下输出 src sentencepiece sentencepiece wra
  • 将打开关闭的 Google Chrome 浏览器添加到 Selenium linkedin_scraper 代码中

    我正在尝试抓取一些知名人士的 LinkedIn 个人资料 该代码获取一堆 LinkedIn 个人资料 URL 然后使用Selenium and scrape linkedin收集信息并将其作为 json 文件保存到文件夹中 我遇到的问题是
  • 引发 RuntimeError(f"目录 '{directory}' 不存在") RuntimeError: 导入 fitz 时目录 'static/' 不存在

    当我运行 extract img py 文件时出现此错误 RuntimeError f 目录 directory 不存在 运行时错误 导入 fitz 时不存在目录 static 我不明白为什么这会给我发回此错误消息 我之前看到过关于这个话题
  • 在 Python 3 中动态导入模块的问题

    我遇到的情况是 在我的 Python 3 项目中 在运行时必须包含某些模块 我在用着importlib import module为了这 第二次更新 我确实找到了一种方法来做一些接近我想要的事情 一些额外的代码可能会使我的一些链接稍微偏离一
  • 将 matplotlib png 转换为 base64 以在 html 模板中查看

    背景 你好 我正在尝试制作一个简单的网络应用程序 按照教程计算阻尼振动方程 并将结果的 png 返回到 html 页面 然后将其转换为 Base64 字符串 Problem 该应用程序运行正常 只是在计算结果时返回损坏的图像图标 可能是因为
  • 绘制“plot”而不是“scatter”时,图例选择会中断

    再会 这个问题是后续问题为什么图例选取仅适用于 ax twinx 而不适用于 ax https stackoverflow com q 60167378 9282844 下面提供的最小代码分别绘制了两条曲线ax1 and ax2 ax1 t
  • Selenium Webdriver - Python - leboncoin - pb 选择带重音的按钮

    我正在尝试在以下网站上自动填写表格 https www leboncoin fr https www leboncoin fr 我用 Selenium IDE 录制了一个脚本 我有一个通过单击 Se 连接器 按钮并填写我的密码和用户名来自动
  • 在函数调用之间保存数据的Pythonic方式是什么?

    对我来说 上下文是我需要在调用修改该值的函数之间保留的单个 int 的信息 我可以使用全局 但我知道这是不鼓励的 现在 我使用了包含 int 的列表形式的默认参数 并利用了可变性 以便在调用之间保留对值的更改 如下所示 def increm
  • 向 Python 2.6 添加 SSL 支持

    我尝试使用sslPython 2 6 中的模块 但我被告知它不可用 安装OpenSSL后 我重新编译2 6 但问题仍然存在 有什么建议么 您安装了 OpenSSL 开发库吗 我必须安装openssl devel例如 在 CentOS 上 在
  • 在ansible中合并字典

    我目前正在构建一个使用 ansible 安装 PHP 的角色 并且在合并字典时遇到一些困难 我尝试了多种方法来做到这一点 但我无法让它像我想要的那样工作 A vars file my default values key value my
  • 在 MATLAB 中创建共享库

    一位研究人员在 MATLAB 中创建了一个小型仿真 我们希望其他人也能使用它 我的计划是进行模拟 清理一些东西并将其变成一组函数 然后我打算将其编译成C库并使用SWIG https en wikipedia org wiki SWIG创建一
  • Floyd-Warshall 算法:获取最短路径

    假设一个图由一个表示n x n维数邻接矩阵 我知道如何获得所有对的最短路径矩阵 但我想知道有没有办法追踪所有最短路径 Blow是python代码实现 v len graph for k in range 0 v for i in range
  • 如何在matplotlib中基于x轴更改直方图颜色

    我有根据 pandas 数据框计算出的直方图 我想根据 x 轴值更改颜色 例如 If the value is 0 the color should be green If the value is gt 0 the color shoul
  • PyQt - 如何检查 QDialog 是否可见?

    我有个问题 我有这个代码 balls Ball for i in range 1 10 因此 当我说 Ball 时 这将在 QDialog 上绘制一个球 然后当这完成后 我正在移动球QDialog无限循环中 我想说类似的话while QDi
  • 网页抓取 - 如何识别网页上的主要内容

    给定一个新闻文章网页 来自任何主要新闻来源 例如时报或彭博社 我想识别该页面上的主要文章内容 并丢弃其他杂项元素 例如广告 菜单 侧边栏 用户评论 在大多数主要新闻网站上都可以使用的通用方法是什么 有哪些好的数据挖掘工具或库 最好是基于Py
  • psutil:测量特定进程的CPU使用率

    我正在尝试测量进程树的 cpu 使用率 目前获取进程 没有子进程 的 cpu usage 就可以了 但我得到了奇怪的结果 import psutil p psutil Process PID p cpu percent 还给我float g
  • Jupyter Notebook 中的多处理与线程

    我试图测试这个例子here https ipywidgets readthedocs io en stable examples Widget 20Asynchronous html将其从线程更改为多处理 在 jupyter Noteboo
  • 如何让你的精灵在pygame中跳跃

    目前我已经制作了一个平台游戏 可以左右移动我的角色 他从地上开始 关于如何让他跳的任何想法 因为我不明白 目前 如果我按住向上键 我的玩家精灵将连续向上移动 或者如果我按下它 我的玩家精灵将向上移动并保持向上 我想找个办法远离他 让我重新跌

随机推荐