【测试】【Debug】pytest运行后print没有输出

python 复制代码
import pytest
def test_good():
    for i in range(1000):
        print(i)

def test_bad():
    print('this should fail!')
    assert False

比如上述程序,运行之后只能看到输出了'this should fail!';但是debug版的测试运行后又能看到test_good函数中的输出。

这是为什么呢?

  1. pytest自动会capture通过的那些测试用例运行中所print出来的output;而failed的那些测试用例则会打印其中运行过的print的输出。
  2. 如果想要获取全部的输出:
    在终端中运行下面其中一行即可 这两种都可以
shell 复制代码
pytest name.py --capture=no     # show print statements in console

pytest name.py -s               # equivalent to previous command
相关推荐
nbwenren5 天前
2026实测:Gemini 3.1 Pro 从需求文档到 pytest 测试用例一条龙教程
测试用例·pytest
我的xiaodoujiao6 天前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest
我的xiaodoujiao6 天前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
技术钱7 天前
PyTest配置与API测试用例
servlet·测试用例·pytest
weixin_419658318 天前
pytest 零基础入门实战指南
python·pycharm·pytest
姚青&11 天前
软件测试基础概念
单元测试·pytest
爆更小哇14 天前
pytest集成Allure报告教程
python·测试工具·pytest·接口测试·allure
gCode Teacher 格码致知15 天前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest
gCode Teacher 格码致知15 天前
Python提高: unittest和 pytest的使用方法-由Deepseek产生
开发语言·python·pytest