【问题标题】:pytest recording results in addition to the pass/failpytest 记录结果除了通过/失败
【发布时间】:2014-10-30 21:55:38
【问题描述】:

我刚刚开始使用 pytest。除了通过/失败状态,还有什么方法可以记录结果吗?

例如,假设我有一个这样的测试函数:

@pytest.fixture(scope="session")
def server():
   # something goes here to setup the server

def test_foo(server):
   server.send_request()
   response = server.get_response()
   assert len(response) == 42

如果响应的长度为 42,则测试通过。但我也想记录响应值(“...出于质量保证目的,将记录此调用....”),甚至虽然我并不严格要求通过/失败标准的确切值。

【问题讨论】:

  • 你想用额外的记录数据做什么?例如。您希望以后能够比较不同的测试运行吗?似乎您想要收集的数据作为单独的套件/事物(例如仪器/集成)可能会更好。如果您不需要任何特别“有趣”的东西,那么将数据保存到平面文件可能是可以接受的?
  • 我只想要一种结构化的方式来存储每个测试的任何辅助数据。是的,我可以做带外方法并自己存储它......我希望 pytest 可以提供一个功能。对于人类来说,只需仔细检查测试结果是否合理。
  • 对于任何单元测试框架,我都没有遇到过这样的事情(并不是说不存在)。但是考虑到如果您想要存储的数据(以及您可能想要将其用于什么)的广泛性质,您最好自己滚动。也许您可以考虑使用测试模块记录器的排列,所有这些都从具有特殊名称的父级继承,并带有一个处理程序,该处理程序将记录到您选择的特殊文件?
  • 也许你需要这样的东西:assert len(response) == 42, "My message" 但它只会在失败时显示

标签: python unit-testing python-2.7 pytest


【解决方案1】:

使用print result, 然后运行py.test -s

-s 告诉 py.test 不要捕获标准输出和标准输出。

调整你的例子:

# test_service.py
# ---------------

def test_request():
    # response = server.get_response()
    response = "{'some':'json'}"
    assert len(response) == 15
    print response, # comma prevents default newline

运行py.test -s 产生

$ py.test -s test_service.py
=========================== test session starts ===========================
platform linux2 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4
collected 1 items 

test_service.py {'some':'json'}.

======================== 1 passed in 0.04 seconds =========================
$

或者改用 python 日志记录

# test_logging.py
# ---------------
import logging
logging.basicConfig(
    filename="logresults.txt", 
    format="%(filename)s:%(lineno)d:%(funcName)s %(message)s") 

def test_request():
    response = "{'some':'json'}"
    # print response, # comma prevents default newline
    logging.warn("{'some':'json'}") # sorry, newline unavoidable
    logging.warn("{'some':'other json'}")

运行py.test 生成机器可读文件logresults.txt

test_logging.py:11:test_request {'some':'json'}
test_logging.py:12:test_request {'some':'other json'}

专业提示

运行 vim logresults.txt +cbuffer 以加载 logresults.txt 作为您的快速修复列表。

【讨论】:

  • 认真的吗?我很欣赏这个建议,但我不建议使用任何语言进行诊断。您的建议对于人类可读的报告来说是可以的,但需要屏幕抓取技术来进行机器解析。如果测试框架没有结构化的方式来保存辅助数据,我可以使用 python logging 模块。
  • 哎呀。错过了关于机器可解析的一点。 Pytest 不保留结果数据库,不将其测试结果作为记录器提供,也不报告测试返回值。辅助测试数据通道为standard out/err。机器可读报告有一个内置选项:--junit-xml=report.xml。假设有一个工具可以处理 junit-xml 并且额外的数据被发送到标准输出或标准错误。
【解决方案2】:

查看我将测试数据传递给 ELK 的示例 http://fruch.github.io/blog/2014/10/30/ELK-is-fun/

后来我把它变成了这样:

def pytest_configure(config):
    # parameter to add analysis from tests teardowns, and etc.
    config.analysis = []

def pytest_unconfigure(config):
    # send config.analysis to where you want, i.e. file / DB / ELK
    send_to_elk(config.analysis)

def test_example():
   pytest.config.analysis += [ "My Data I want to keep" ]

这是每个运行/会话数据,而不是每个测试(但我正在研究如何在每个测试中执行此操作)

一旦我有一个工作示例,我会尝试更新...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-29
    • 2020-04-10
    • 1970-01-01
    • 2011-05-12
    • 2021-07-28
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多