Python 除通过/失败外,pytest记录结果

Python 除通过/失败外,pytest记录结果,python,unit-testing,python-2.7,pytest,Python,Unit Testing,Python 2.7,Pytest,我刚刚开始使用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(respon

我刚刚开始使用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
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" ]
如果响应长度为42,则测试通过。但我也希望记录响应值(“此调用将被记录以用于质量保证目的…”),尽管我并不严格要求通过/失败标准的精确值。

使用
打印结果,
然后运行
py.test-s
-s
告诉py.test不要捕获stdout和stdout

调整您的示例:

# 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\u service.py
========================================测试会话开始===========================
平台linux2--Python2.7.6--py-1.4.26--pytest-2.6.4
收集1项
test_service.py{'some':'json'}。
=====================================1在0.04秒内通过=========================
$
或者改用python日志记录 运行
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作为快速修复列表

参见我向麋鹿传递测试数据的示例

后来我做的有点像这样:

@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
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" ]
这是每个运行/会话的数据,而不是每个测试(但我正在研究如何在每个测试中执行)


一旦我有了一个工作示例,我将尝试更新…

您想对额外的记录数据做什么?例如,您希望以后能够比较不同的测试运行吗?似乎您想要收集的数据最好作为一个单独的套件/东西(例如,仪器/集成)使用。如果您不需要任何特别“有趣”的东西,那么将数据保存到平面文件可能是可以接受的?我只需要一种结构化的方式来存储每个测试的任何辅助数据。是的,我可以自己做带外的方法并保存它。。。我希望pytest可以提供一个特性。对于一个人来说,只要仔细检查一下测试结果是否合理就可以了。对于任何单元测试框架,我都没有遇到过这样的事情(更不用说没有)。但是考虑到你想要存储的数据(以及你可能想要使用它的目的)的广泛性质,你最好还是自己动手。也许你可以考虑使用一个测试模块记录器的安排,所有的继承都是从一个特殊的父代继承的,处理程序会登录到你选择的一个特殊文件中。也许你需要这样的东西:<代码> Asdit LeN(Read)=42,“我的消息”< /Cord>,但是它只会严重地显示故障吗?我很感激你的建议,但我不建议用任何语言进行诊断。您的建议对于人类可读的报告是可以的,但需要使用屏幕抓取技术进行机器解析。如果测试框架没有结构化的方法来保存辅助数据,我可以使用python
日志记录模块。错过了一点关于机器可分析的东西。Pytest不保留结果数据库,不将其测试结果作为记录器提供,也不报告测试返回值。其辅助测试数据通道为。机器可读报告有一个内置选项:
--junitxml=report.xml
。这假设有一个处理JUnitXML的工具,额外的数据被发送到standard out或standard error。