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(response) == 42
如果响应的长度是42,这个测试就算通过。但是我还想记录一下响应的值(“...这个调用会被记录以确保质量...”),尽管我并不严格要求这个值来判断测试是否通过或失败。
2 个回答
0
看看我如何将测试数据传递给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" ]
这是每次运行或会话的数据,而不是每个测试的数据(不过我正在努力弄清楚如何做到每个测试的数据)
等我有了一个可以工作的例子后,我会尝试更新...
0
使用 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 加载为你的快速修复列表。