测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----如何捕获标准输出和标准错误输出
收藏本文
作者:redrose2100 类别: 日期:2022-05-13 17:56:05 阅读:1128 次 消耗积分:0 分
## 一、设置标准输出标准错误输出的模式 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代码如下: ```python import sys import os def test_demo(): print("in test_demo...") os.system("dir") sys.stdout.write("hello\n") assert 1==2 ``` (1)使用pytest命令执行,此时关闭实时输出,Capture Log捕获所有的标准输出 如下,可以看到Capture Log既有sys.out的输出,也有执行系统命令的输出(执行系统命令的输出不是sys.out,而是文件描述符1) ```bash $ 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 __________________________________ def test_demo(): print("in test_demo...") os.system("dir") sys.stdout.write("hello\n") > assert 1==2 E assert 1 == 2 test_demo.py:8: AssertionError ---------------------------- Captured stdout call ----------------------------- in test_demo... Volume in drive D is work Volume Serial Number is 84B0-F6B7 Directory of D:\src\blog\tests 2021/12/27 09:38
. 2021/12/27 09:38
.. 2021/12/24 17:03
.pytest_cache 2021/12/27 09:32 16 1 2021/12/23 15:31 168 conftest.py 2021/12/22 17:13 300 pytest.ini 2021/12/16 09:21 1,797 red_blue_ball_ticket.py 2021/12/27 09:38 143 test_demo.py 2021/12/24 15:38 0 __init__.py 2021/12/27 09:38
__pycache__ 6 File(s) 2,424 bytes 4 Dir(s) 165,265,436,672 bytes free hello =========================== short test summary info =========================== FAILED test_demo.py::test_demo - assert 1 == 2 ============================== 1 failed in 0.06s ============================== ``` (2)使用pytest -s 命令,此时打开实时输出,关闭Capture Log输出 如下,只有实时输出,没有Captured stdout call内容 ```bash $ pytest -s ============================= 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 Volume in drive D is work Volume Serial Number is 84B0-F6B7 Directory of D:\src\blog\tests 2021/12/27 09:38
. 2021/12/27 09:38
.. 2021/12/24 17:03
.pytest_cache 2021/12/27 09:32 16 1 2021/12/23 15:31 168 conftest.py 2021/12/22 17:13 300 pytest.ini 2021/12/16 09:21 1,797 red_blue_ball_ticket.py 2021/12/27 09:38 143 test_demo.py 2021/12/24 15:38 0 __init__.py 2021/12/27 09:38
__pycache__ 6 File(s) 2,424 bytes 4 Dir(s) 165,265,436,672 bytes free in test_demo... hello F ================================== FAILURES =================================== __________________________________ test_demo __________________________________ def test_demo(): print("in test_demo...") os.system("dir") sys.stdout.write("hello\n") > assert 1==2 E assert 1 == 2 test_demo.py:8: AssertionError =========================== short test summary info =========================== FAILED test_demo.py::test_demo - assert 1 == 2 ============================== 1 failed in 0.06s ============================== ``` (3)使用pytest --capture=sys 命令时打开实时输出,Capture Log只输出sys.out的内容 如下,Capture Log中只有sys.out的内容,没有执行系统命令的输出 ```bash $ pytest --capture=sys ============================= 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 Volume in drive D is work Volume Serial Number is 84B0-F6B7 Directory of D:\src\blog\tests 2021/12/27 09:38
. 2021/12/27 09:38
.. 2021/12/24 17:03
.pytest_cache 2021/12/27 09:32 16 1 2021/12/23 15:31 168 conftest.py 2021/12/22 17:13 300 pytest.ini 2021/12/16 09:21 1,797 red_blue_ball_ticket.py 2021/12/27 09:38 143 test_demo.py 2021/12/24 15:38 0 __init__.py 2021/12/27 09:38
__pycache__ 6 File(s) 2,424 bytes 4 Dir(s) 165,265,432,576 bytes free F [100%] ================================== FAILURES =================================== __________________________________ test_demo __________________________________ def test_demo(): print("in test_demo...") os.system("dir") sys.stdout.write("hello\n") > assert 1==2 E assert 1 == 2 test_demo.py:8: AssertionError ---------------------------- Captured stdout call ----------------------------- in test_demo... hello =========================== short test summary info =========================== FAILED test_demo.py::test_demo - assert 1 == 2 ============================== 1 failed in 0.06s ============================== ``` (4)使用 pytest --capture=fd 命令关闭实时输出,Captrue Log捕获所有的标准输出 ```bash $ pytest --capture=fd ============================= 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 __________________________________ def test_demo(): print("in test_demo...") os.system("dir") sys.stdout.write("hello\n") > assert 1==2 E assert 1 == 2 test_demo.py:8: AssertionError ---------------------------- Captured stdout call ----------------------------- in test_demo... Volume in drive D is work Volume Serial Number is 84B0-F6B7 Directory of D:\src\blog\tests 2021/12/27 09:38
. 2021/12/27 09:38
.. 2021/12/24 17:03
.pytest_cache 2021/12/27 09:32 16 1 2021/12/23 15:31 168 conftest.py 2021/12/22 17:13 300 pytest.ini 2021/12/16 09:21 1,797 red_blue_ball_ticket.py 2021/12/27 09:38 143 test_demo.py 2021/12/24 15:38 0 __init__.py 2021/12/27 09:38
__pycache__ 6 File(s) 2,424 bytes 4 Dir(s) 165,265,432,576 bytes free hello =========================== short test summary info =========================== FAILED test_demo.py::test_demo - assert 1 == 2 ============================== 1 failed in 0.06s ============================== ``` (5)使用pytest --capture=tee-sys命令,相当于pytest -s 和 pytest --capture=sys的结合,即打开实时输出,同时Captrue Log只捕获sys.out,sys.err ```bash $ pytest --capture=tee-sys ============================= 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 Volume in drive D is work Volume Serial Number is 84B0-F6B7 Directory of D:\src\blog\tests 2021/12/27 09:38
. 2021/12/27 09:38
.. 2021/12/24 17:03
.pytest_cache 2021/12/27 09:32 16 1 2021/12/23 15:31 168 conftest.py 2021/12/22 17:13 300 pytest.ini 2021/12/16 09:21 1,797 red_blue_ball_ticket.py 2021/12/27 09:38 143 test_demo.py 2021/12/24 15:38 0 __init__.py 2021/12/27 09:38
__pycache__ 6 File(s) 2,424 bytes 4 Dir(s) 165,265,428,480 bytes free in test_demo... hello F [100%] ================================== FAILURES =================================== __________________________________ test_demo __________________________________ def test_demo(): print("in test_demo...") os.system("dir") sys.stdout.write("hello\n") > assert 1==2 E assert 1 == 2 test_demo.py:8: AssertionError ---------------------------- Captured stdout call ----------------------------- in test_demo... hello =========================== short test summary info =========================== FAILED test_demo.py::test_demo - assert 1 == 2 ============================== 1 failed in 0.06s ============================== ``` ## 二、在测试函数中捕获标准输出标砖错误输出 用于捕获标准输出有如下四个fixture:capsys, capsysbinary, capfd,和capfdbinary,capsys用于获取sys.out中的内容,当sys.out中的内容为二进制数据时使用capsysbinary,同样,capfd用于获取系统级标准输出,即文件描述符1和2,当数据为二进制时使用和capfdbinary test_demo.py代码如下: ```python def test_demo(capsys): print("hello world") captured = capsys.readouterr() assert "hello world hahaha" in captured.out ``` 执行结果如下,可以发现这里能将上面打印语句的内容捕获到,即"hello world\n",这里断言报错主要是通过报错信息查看真是额返回值 ```bash $ 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的时候关闭捕获 ```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++的打印 ```bash $ 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 ============================== ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/191
上一篇:
Pytest----如何正确使用pytest的日志功能
下一篇:
Pytest----如何使用skip和xfail
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件