Pytest----如何使用--capture参数设置实时打印及Capture Log打印
目录
- 一、设置标准输出标准错误输出的模式
- 1.1 pytest命令
- 1.2 pytest -s 命令
- 1.3 pytest --capture=sys 命令
- 1.4 pytest --capture=fd 命令
- 1.5 pytest --capture=tee-sys 命令
- 二、在测试函数中捕获标准输出标砖错误输出
一、设置标准输出标准错误输出的模式
pytest捕获标准输出标准错误输出的模式主要有以下几种
- pytest :和pytest --capture=fd 模式是一样的,默认的就是pytest --capture=fd 模式
- pytest -s :打开实时输出,关闭Capture Log输出,
- pytest --capture=sys :打开实时输出,Captrue Log只捕获sys.out,sys.err
- pytest --capture=fd :关闭实时输出,Captrue Log捕获所有的标准输出
- pytest --capture=tee-sys :pytest -s 和 pytest --capture=sys的结合,即打开实时输出,同时Captrue Log只捕获sys.out,sys.err
test_demo.py代码如下:
import sys
import os
def test_demo():
print("in test_demo...")
os.system("dir")
sys.stdout.write("hello\n")
assert 1==2
1.1 pytest命令
使用pytest命令执行,此时关闭实时输出,Capture Log捕获所有的标准输出
如下,可以看到Capture Log既有sys.out的输出,也有执行系统命令的输出(执行系统命令的输出不是sys.out,而是文件描述符1),此外这里需要说明的是Capture Log是只有在用例失败的情况下才会打印的,因此当用例通过的时候,是不会显示Capture Log,而pytest命令时关闭实时输出的,因此在用例通过的时候回显中是看不到执行命令的结果的,如下图所示红框1中没有命令行的结果,这就是因为pytest关闭了实时输出,红框2中有命令行的结果,是因为用例失败了,所以会显示Capture Log的内容,而Capture Log的内容在默认情况下或者 --capture=fd 情况下是采集所有的输出的。

1.2 pytest -s 命令
使用pytest -s 命令,此时打开实时输出,实时输出显示所有内容,关闭Capture Log输出
如下,可以看到红框1中是有实时输出的,即将命令的执行结果都显示出来了,而红框2的位置虽然此时用例也是白了,但是没有Captured Log的内容,即pytest -s 命令时打开了实时输出,而关闭了Capture Log输出

1.3 pytest --capture=sys 命令
使用pytest --capture=sys 命令时打开实时输出,Capture Log只输出sys.out的内容
如下,可以看到红框1中为实时输出,此时实时是显示执行命令的回显,但是sys.stdout中的内容在实时输出的部分没有显示,此外在红框2中可以看出因为用例失败,所以会显示Capture Log,但此时Capture Log中只有sys.stdout的内容,没有执行系统命令的回显

1.4 pytest --capture=fd 命令
在1.1中已经提到过,pytest --capture=fd 命令和pytest命令时一样的,即关闭实时输出,在报错的时候显示Captrue Log,并且Capture Log捕获所有的标准输出,包括sys.stdout和文件描述符(fd)中的内容

1.5 pytest --capture=tee-sys 命令
使用pytest --capture=tee-sys命令,即打开实时输出,实时输出显示所有内容,同时在用例报错的时候也显示Captrue Log,但Capture Log只捕获sys.stdout,如下,红框1中即为实时打印,将sys.stdout和文件描述符(fd)中的内容均打印出来了,而红框2即为Capture Log,此时只显示了sys.stdout中的内容

二、在测试函数中捕获标准输出标砖错误输出
用于捕获标准输出有如下四个fixture:capsys, capsysbinary, capfd,和capfdbinary,capsys用于获取sys.out中的内容,当sys.out中的内容为二进制数据时使用capsysbinary,同样,capfd用于获取系统级标准输出,即文件描述符1和2,当数据为二进制时使用和capfdbinary
test_demo.py代码如下:
def test_demo(capsys):
print("hello world")
captured = capsys.readouterr()
assert "hello world hahaha" in captured.out
执行结果如下,可以发现这里能将上面打印语句的内容捕获到,即"hello world\n",这里断言报错主要是通过报错信息查看真是额返回值
$ pytest
============================= test session starts =============================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item
test_demo.py F [100%]
================================== FAILURES ===================================
__________________________________ test_demo __________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x00000226B8916D00>
def test_demo(capsys):
print("hello world")
captured = capsys.readouterr()
> assert "hello worldd" in captured.out
E AssertionError: assert 'hello worldd' in 'hello world\n'
E + where 'hello world\n' = CaptureResult(out='hello world\n', err='').out
test_demo.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_demo.py::test_demo - AssertionError: assert 'hello worldd' in 'he...
============================== 1 failed in 0.05s ==============================
还可以设置对指定的内容关闭capture捕获,如下
test_demo.py代码如下,设置当打印hello python的时候关闭捕获
def test_demo(capsys):
print("hello world")
with capsys.disabled():
print("hello python")
print("hello C++")
captured = capsys.readouterr()
assert "hello world" in captured.out
assert "hello C++" in captured.out
assert "hello python" in captured.out
执行结果如下,可以发现,这里确实只捕获了hello world 和hello C++的打印
$ pytest
============================= test session starts =============================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item
test_demo.py hello python
F [100%]
================================== FAILURES ===================================
__________________________________ test_demo __________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x00000294FFB444C0>
def test_demo(capsys):
print("hello world")
with capsys.disabled():
print("hello python")
print("hello C++")
captured = capsys.readouterr()
assert "hello world" in captured.out
assert "hello C++" in captured.out
> assert "hello python" in captured.out
E AssertionError: assert 'hello python' in 'hello world\nhello C++\n'
E + where 'hello world\nhello C++\n' = CaptureResult(out='hello world\nhello C++\n', err='').out
test_demo.py:10: AssertionError
=========================== short test summary info ===========================
FAILED test_demo.py::test_demo - AssertionError: assert 'hello python' in 'he...
============================== 1 failed in 0.06s ==============================
相关推荐
chromedriver-win64-151.0.7897.3(Canary).zip
chromedriver-win64-151.0.7897.3(Canary).zip
Awesome LLM Apps开源 AI 智能体、智能体技能与 RAG 应用
集结了采用 RAG 技术的杰出大型语言模型应用,涵盖 OpenAI、Anthropic、Gemini 以及开源模型。
政府科技管理者如何提升区域科技成果转化成功率?.docx
政府科技管理者如何提升区域科技成果转化成功率?
LangGraph与Graphviz集成:生成高质量静态图
内容概要:展示如何将LangGraph图导出为Graphviz DOT格式,生成PNG/SVG。通过自定义布局和样式,呈现清晰的流程图。对比Mermaid,讨论适用场景。 0533zbyy.com 0595ty.cn www.zh-casting.com yobest2.com 10cd.cn
科技中介服务机构如何通过智能化工具快速识别高价值技术成果?.docx
科技中介服务机构如何通过智能化工具快速识别高价值技术成果?


