Pytest----如何使用--capture参数设置实时打印及Capture Log打印

DevOps技术社区 2022-08-28 11:28:03

目录

  • 一、设置标准输出标准错误输出的模式
  • 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 ==============================

...全文
1991 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

32

社区成员

发帖
与我相关
我的任务
社区描述
围绕测试开发、Devops、软件工程等方向,学习交流,共享技术新动态、新方向。求助工作中的问题,困惑等,互相交流,共同成长
软件工程测试工具devops 技术论坛(原bbs) 江苏省·南京市
社区管理员
  • redrose2100
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

1、社区话题和文章主要围绕DevOps技术栈

2、有广告需求请联系微信 Redrose2100

 

试试用AI创作助手写篇文章吧