如何通过Pytest实现并查看详细的控制台输出信息?

2026-05-24 18:581阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计256个文字,预计阅读时间需要2分钟。

如何通过Pytest实现并查看详细的控制台输出信息?

运行一个简单的示例:启动一个计数器程序,每秒增加计数,直到达到100。程序界面简洁,用户可通过按钮开始和停止计数。

运行一个简单的用例:

#cd code/ch1/test_one.py
def test_passing():
assert (1, 2, 3) == (1, 2, 3)

运行结果及说明:

如何通过Pytest实现并查看详细的控制台输出信息?

测试运行可能出现的结果总结:

举例:

import pytest
#测试通过
def test_passing():
assert (1, 2, 3) == (1, 2, 3)

#测试失败
def test_failing():
assert (1, 2, 3) == (3, 2, 1)

#跳过不执行
@pytest.mark.skip()
def test_skip():
assert (1, 2, 3) == (3, 2, 1)


#预期失败,确实失败
@pytest.mark.xfail()
def test_xfail():
assert (1, 2, 3) == (3, 2, 1)


#预期失败,但是结果pass
@pytest.mark.xfail()
def test_xpass():
assert (1, 2, 3) == (1, 2, 3)

运行结果:

去期待陌生,去拥抱惊喜。

本文共计256个文字,预计阅读时间需要2分钟。

如何通过Pytest实现并查看详细的控制台输出信息?

运行一个简单的示例:启动一个计数器程序,每秒增加计数,直到达到100。程序界面简洁,用户可通过按钮开始和停止计数。

运行一个简单的用例:

#cd code/ch1/test_one.py
def test_passing():
assert (1, 2, 3) == (1, 2, 3)

运行结果及说明:

如何通过Pytest实现并查看详细的控制台输出信息?

测试运行可能出现的结果总结:

举例:

import pytest
#测试通过
def test_passing():
assert (1, 2, 3) == (1, 2, 3)

#测试失败
def test_failing():
assert (1, 2, 3) == (3, 2, 1)

#跳过不执行
@pytest.mark.skip()
def test_skip():
assert (1, 2, 3) == (3, 2, 1)


#预期失败,确实失败
@pytest.mark.xfail()
def test_xfail():
assert (1, 2, 3) == (3, 2, 1)


#预期失败,但是结果pass
@pytest.mark.xfail()
def test_xpass():
assert (1, 2, 3) == (1, 2, 3)

运行结果:

去期待陌生,去拥抱惊喜。