【测试】【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
相关推荐
姚青&11 天前
PyCharm 配置与界面化运行
pytest
姚青&11 天前
Pytest 测试用例断言
测试用例·pytest
姚青&11 天前
Pytest 命名规则
pytest
姚青&11 天前
Pytest 测试用例结构
测试用例·pytest
姚青&11 天前
Pytest 简介、安装与环境准备
pytest
测试渣12 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
小凯1234513 天前
pytest框架-详解(学习pytest框架这一篇就够了)
python·学习·pytest
逻极13 天前
pytest 入门指南:Python 测试框架从零到一(2025 实战版)
开发语言·python·pytest