Pytest----如何捕获标准输出和标准错误输出
作者:redrose2100   类别:    日期:2022-05-13 17:56:05    阅读:1350 次   消耗积分: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代码如下:

  1. import sys
  2. import os
  3. def test_demo():
  4. print("in test_demo...")
  5. os.system("dir")
  6. sys.stdout.write("hello\n")
  7. assert 1==2

(1)使用pytest命令执行,此时关闭实时输出,Capture Log捕获所有的标准输出

如下,可以看到Capture Log既有sys.out的输出,也有执行系统命令的输出(执行系统命令的输出不是sys.out,而是文件描述符1)

  1. $ pytest
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py F [100%]
  8. ================================== FAILURES ===================================
  9. __________________________________ test_demo __________________________________
  10. def test_demo():
  11. print("in test_demo...")
  12. os.system("dir")
  13. sys.stdout.write("hello\n")
  14. > assert 1==2
  15. E assert 1 == 2
  16. test_demo.py:8: AssertionError
  17. ---------------------------- Captured stdout call -----------------------------
  18. in test_demo...
  19. Volume in drive D is work
  20. Volume Serial Number is 84B0-F6B7
  21. Directory of D:\src\blog\tests
  22. 2021/12/27 09:38 <DIR> .
  23. 2021/12/27 09:38 <DIR> ..
  24. 2021/12/24 17:03 <DIR> .pytest_cache
  25. 2021/12/27 09:32 16 1
  26. 2021/12/23 15:31 168 conftest.py
  27. 2021/12/22 17:13 300 pytest.ini
  28. 2021/12/16 09:21 1,797 red_blue_ball_ticket.py
  29. 2021/12/27 09:38 143 test_demo.py
  30. 2021/12/24 15:38 0 __init__.py
  31. 2021/12/27 09:38 <DIR> __pycache__
  32. 6 File(s) 2,424 bytes
  33. 4 Dir(s) 165,265,436,672 bytes free
  34. hello
  35. =========================== short test summary info ===========================
  36. FAILED test_demo.py::test_demo - assert 1 == 2
  37. ============================== 1 failed in 0.06s ==============================

(2)使用pytest -s 命令,此时打开实时输出,关闭Capture Log输出

如下,只有实时输出,没有Captured stdout call内容

  1. $ pytest -s
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py Volume in drive D is work
  8. Volume Serial Number is 84B0-F6B7
  9. Directory of D:\src\blog\tests
  10. 2021/12/27 09:38 <DIR> .
  11. 2021/12/27 09:38 <DIR> ..
  12. 2021/12/24 17:03 <DIR> .pytest_cache
  13. 2021/12/27 09:32 16 1
  14. 2021/12/23 15:31 168 conftest.py
  15. 2021/12/22 17:13 300 pytest.ini
  16. 2021/12/16 09:21 1,797 red_blue_ball_ticket.py
  17. 2021/12/27 09:38 143 test_demo.py
  18. 2021/12/24 15:38 0 __init__.py
  19. 2021/12/27 09:38 <DIR> __pycache__
  20. 6 File(s) 2,424 bytes
  21. 4 Dir(s) 165,265,436,672 bytes free
  22. in test_demo...
  23. hello
  24. F
  25. ================================== FAILURES ===================================
  26. __________________________________ test_demo __________________________________
  27. def test_demo():
  28. print("in test_demo...")
  29. os.system("dir")
  30. sys.stdout.write("hello\n")
  31. > assert 1==2
  32. E assert 1 == 2
  33. test_demo.py:8: AssertionError
  34. =========================== short test summary info ===========================
  35. FAILED test_demo.py::test_demo - assert 1 == 2
  36. ============================== 1 failed in 0.06s ==============================

(3)使用pytest —capture=sys 命令时打开实时输出,Capture Log只输出sys.out的内容

如下,Capture Log中只有sys.out的内容,没有执行系统命令的输出

  1. $ pytest --capture=sys
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py Volume in drive D is work
  8. Volume Serial Number is 84B0-F6B7
  9. Directory of D:\src\blog\tests
  10. 2021/12/27 09:38 <DIR> .
  11. 2021/12/27 09:38 <DIR> ..
  12. 2021/12/24 17:03 <DIR> .pytest_cache
  13. 2021/12/27 09:32 16 1
  14. 2021/12/23 15:31 168 conftest.py
  15. 2021/12/22 17:13 300 pytest.ini
  16. 2021/12/16 09:21 1,797 red_blue_ball_ticket.py
  17. 2021/12/27 09:38 143 test_demo.py
  18. 2021/12/24 15:38 0 __init__.py
  19. 2021/12/27 09:38 <DIR> __pycache__
  20. 6 File(s) 2,424 bytes
  21. 4 Dir(s) 165,265,432,576 bytes free
  22. F [100%]
  23. ================================== FAILURES ===================================
  24. __________________________________ test_demo __________________________________
  25. def test_demo():
  26. print("in test_demo...")
  27. os.system("dir")
  28. sys.stdout.write("hello\n")
  29. > assert 1==2
  30. E assert 1 == 2
  31. test_demo.py:8: AssertionError
  32. ---------------------------- Captured stdout call -----------------------------
  33. in test_demo...
  34. hello
  35. =========================== short test summary info ===========================
  36. FAILED test_demo.py::test_demo - assert 1 == 2
  37. ============================== 1 failed in 0.06s ==============================

(4)使用 pytest —capture=fd 命令关闭实时输出,Captrue Log捕获所有的标准输出

  1. $ pytest --capture=fd
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py F [100%]
  8. ================================== FAILURES ===================================
  9. __________________________________ test_demo __________________________________
  10. def test_demo():
  11. print("in test_demo...")
  12. os.system("dir")
  13. sys.stdout.write("hello\n")
  14. > assert 1==2
  15. E assert 1 == 2
  16. test_demo.py:8: AssertionError
  17. ---------------------------- Captured stdout call -----------------------------
  18. in test_demo...
  19. Volume in drive D is work
  20. Volume Serial Number is 84B0-F6B7
  21. Directory of D:\src\blog\tests
  22. 2021/12/27 09:38 <DIR> .
  23. 2021/12/27 09:38 <DIR> ..
  24. 2021/12/24 17:03 <DIR> .pytest_cache
  25. 2021/12/27 09:32 16 1
  26. 2021/12/23 15:31 168 conftest.py
  27. 2021/12/22 17:13 300 pytest.ini
  28. 2021/12/16 09:21 1,797 red_blue_ball_ticket.py
  29. 2021/12/27 09:38 143 test_demo.py
  30. 2021/12/24 15:38 0 __init__.py
  31. 2021/12/27 09:38 <DIR> __pycache__
  32. 6 File(s) 2,424 bytes
  33. 4 Dir(s) 165,265,432,576 bytes free
  34. hello
  35. =========================== short test summary info ===========================
  36. FAILED test_demo.py::test_demo - assert 1 == 2
  37. ============================== 1 failed in 0.06s ==============================

(5)使用pytest —capture=tee-sys命令,相当于pytest -s 和 pytest —capture=sys的结合,即打开实时输出,同时Captrue Log只捕获sys.out,sys.err

  1. $ pytest --capture=tee-sys
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py Volume in drive D is work
  8. Volume Serial Number is 84B0-F6B7
  9. Directory of D:\src\blog\tests
  10. 2021/12/27 09:38 <DIR> .
  11. 2021/12/27 09:38 <DIR> ..
  12. 2021/12/24 17:03 <DIR> .pytest_cache
  13. 2021/12/27 09:32 16 1
  14. 2021/12/23 15:31 168 conftest.py
  15. 2021/12/22 17:13 300 pytest.ini
  16. 2021/12/16 09:21 1,797 red_blue_ball_ticket.py
  17. 2021/12/27 09:38 143 test_demo.py
  18. 2021/12/24 15:38 0 __init__.py
  19. 2021/12/27 09:38 <DIR> __pycache__
  20. 6 File(s) 2,424 bytes
  21. 4 Dir(s) 165,265,428,480 bytes free
  22. in test_demo...
  23. hello
  24. F [100%]
  25. ================================== FAILURES ===================================
  26. __________________________________ test_demo __________________________________
  27. def test_demo():
  28. print("in test_demo...")
  29. os.system("dir")
  30. sys.stdout.write("hello\n")
  31. > assert 1==2
  32. E assert 1 == 2
  33. test_demo.py:8: AssertionError
  34. ---------------------------- Captured stdout call -----------------------------
  35. in test_demo...
  36. hello
  37. =========================== short test summary info ===========================
  38. FAILED test_demo.py::test_demo - assert 1 == 2
  39. ============================== 1 failed in 0.06s ==============================

二、在测试函数中捕获标准输出标砖错误输出

用于捕获标准输出有如下四个fixture:capsys, capsysbinary, capfd,和capfdbinary,capsys用于获取sys.out中的内容,当sys.out中的内容为二进制数据时使用capsysbinary,同样,capfd用于获取系统级标准输出,即文件描述符1和2,当数据为二进制时使用和capfdbinary

test_demo.py代码如下:

  1. def test_demo(capsys):
  2. print("hello world")
  3. captured = capsys.readouterr()
  4. assert "hello world hahaha" in captured.out

执行结果如下,可以发现这里能将上面打印语句的内容捕获到,即”hello world\n”,这里断言报错主要是通过报错信息查看真是额返回值

  1. $ pytest
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py F [100%]
  8. ================================== FAILURES ===================================
  9. __________________________________ test_demo __________________________________
  10. capsys = <_pytest.capture.CaptureFixture object at 0x00000226B8916D00>
  11. def test_demo(capsys):
  12. print("hello world")
  13. captured = capsys.readouterr()
  14. > assert "hello worldd" in captured.out
  15. E AssertionError: assert 'hello worldd' in 'hello world\n'
  16. E + where 'hello world\n' = CaptureResult(out='hello world\n', err='').out
  17. test_demo.py:5: AssertionError
  18. =========================== short test summary info ===========================
  19. FAILED test_demo.py::test_demo - AssertionError: assert 'hello worldd' in 'he...
  20. ============================== 1 failed in 0.05s ==============================

还可以设置对指定的内容关闭capture捕获,如下

test_demo.py代码如下,设置当打印hello python的时候关闭捕获

  1. def test_demo(capsys):
  2. print("hello world")
  3. with capsys.disabled():
  4. print("hello python")
  5. print("hello C++")
  6. captured = capsys.readouterr()
  7. assert "hello world" in captured.out
  8. assert "hello C++" in captured.out
  9. assert "hello python" in captured.out

执行结果如下,可以发现,这里确实只捕获了hello world 和hello C++的打印

  1. $ pytest
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
  4. rootdir: D:\src\blog\tests, configfile: pytest.ini
  5. 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
  6. collected 1 item
  7. test_demo.py hello python
  8. F [100%]
  9. ================================== FAILURES ===================================
  10. __________________________________ test_demo __________________________________
  11. capsys = <_pytest.capture.CaptureFixture object at 0x00000294FFB444C0>
  12. def test_demo(capsys):
  13. print("hello world")
  14. with capsys.disabled():
  15. print("hello python")
  16. print("hello C++")
  17. captured = capsys.readouterr()
  18. assert "hello world" in captured.out
  19. assert "hello C++" in captured.out
  20. > assert "hello python" in captured.out
  21. E AssertionError: assert 'hello python' in 'hello world\nhello C++\n'
  22. E + where 'hello world\nhello C++\n' = CaptureResult(out='hello world\nhello C++\n', err='').out
  23. test_demo.py:10: AssertionError
  24. =========================== short test summary info ===========================
  25. FAILED test_demo.py::test_demo - AssertionError: assert 'hello python' in 'he...
  26. ============================== 1 failed in 0.06s ==============================
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/191
个人成就
  • 2022年 : 371 篇 
  • 2023年 : 211 篇 
  • 2024年 : 31 篇 
  • 2025年 : 0 篇 
  • 博客总数: 613 
  • 阅读总量: 639665 
测试开发技术全栈公众号
DevOps技术交流微信群