1、命令行启动
pytest框架是python经典单元测试框架,运行会自动识别测试项目中的测试用例,包含测试模块module(以
test_
开头)、测试类class(以Test
开头)、测试函数function(以test
开头)
命令: pytest [options] [file_or_dir] [file_or_dir] [...]
指定当前项目下的测试所有module:
pytest [option] .
指定某个文件:
pytest [option] test1.py
指定某个测试类:
pytest [option] test1.py::TestCaseClass
指定某个测试函数:
pytest [option] test1.py::TestCaseClass::test_func
常用option参数:
- -v: 显示测试的详细信息
- -s: 打印测试函数用例中的print信息,方便调试
- -q: 与-v相反, 静默输出信息
- -k: 允许使用表达式指定希望执行的测试用例, 批量执行用例, 例如: demo.py有三个测试用例函数,分别为: test_result、test_answer、test_func,
pytest -s -v -k "func or result" dem