ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

04、pytest运行多个测试用例

2026/8/16 9:34:48 拓冰建站 浏览量
04、pytest运行多个测试用例

官方用例

目录结构

course_04
|
|----subdir
|		|
|		|----sample03_test.py
|		|
|		|----test_sample04.py
|
|----sample02_test.py
|
|----test_sample01.py
# content of test_sample01.pydef test_simple01():print("test simple01")assert 0
# content of test_sample02_test.pydef test_simple02():print("test simple02")assert 0
# content of sample03_test.pydef test_simple03():print("test simple03")assert 0
# content of test_sample04.pydef test_simple04():print("test simple04")assert 0

在这里插入图片描述

场景应用

​ pytest将运行当前目录及其子目录中所有形式为test_*.py或*_test.py的文件。更一般地说,它遵循标准的测试发现规则。

其它想法

​ 项目测试中,一个项目编写一个测试目录,每个大模块编写一个test_<模块名>.py文件,每个模块中每条测试用例编写一个test_<用例名>函数,每次回归时,在测试目录下执行pytest,并查看运行效果。