1. 控制台执行方式

1.1 指定某个模块

pytest test_module.py

1.2 指定某个目录及其子目录的所有测试文件(以test_ 开头或者_test结尾的方法或者文件)

pytest testcase

1.3 指定某个某块的某个方法

pytest test_file::test_case
test_file,为.py文件
test_case, 为test_file.py中的一个测试用例 

1.4 指定执行某模块的某个类中的某个用例

 pytest test_model.py::test_class::test_method

1.5 指定执行当前目录下满足某个条件的测试用例

pytest -k "MyName and not Age"

执行当前目录下,名字包含MyName但不包含Age的测试用例
例如:会执行测试用例TestMyName.test_something 但不会运行TestMyName.test_Age_simple

1.6  执行第N次失败后停止运行

pytest -x  # 遇到第一个失败时,停止
pytest --maxfail==2  # 遇到第二个失败时,停止

1.7  运行某个包

pytest --pyargs pkg.testing
这将导入pkg.testing并使用其文件系统位置来查找并运行测试

1.8  运行中定位错误

在执⾏的时候出错,我们希望能够快速定位出具体是哪⼀⾏出错了,就会使⽤到--lf,命令为
python -m pytest -v --lf tests/test_add.py

1.9 忽略执行

python -m pytest -v -rs tests/test_add.py

-rs 在编写的测试点中,有的测试⽤例我们是忽略执⾏的

@pytest.mark.skip(reason="该功能已经取消")
def test_add_004():
    pass
  python -m pytest -v -rs tests/test_add.py

 

 

2. 在代码里面方式

在main方法中执行pytest.main(),将会自动查找并执行当前目录下,以test_开头的文件或者以_test结尾的py文件。

例如:

设置pytest的执行参数 pytest.main(['--html=./report.html','test_login.py'])

执行test_login.py文件,并生成html格式的报告

 

3. 报告中错误类型

Logo

一站式 AI 云服务平台

更多推荐