目录 简介 安装 验证安装成功 pytest用例编写规则 pytest执行方式 pytest 在当前目录下运行所有测试 pytest test_mod.py 执行指定的测试文件 pytest somepath 在指定路径下运行所有测试 pytest -k stringexpr 当测试文件、测试类名、测试方法中包含stringexpr关键字时,均可以被执行 pytest test_mod.py::test_func 仅运行与节点ID匹配的测试 pytest常用命令 pytest测试报告 编辑失败重跑 setup和teardown函数 控制测试函数的运行顺序 pytest.ini:通过配置文件配置要执行的测试用例 @pytest.fixture 方法和参数 作为参数引用 作为函数引用 设置自动执行 设置作用域 作用域为function 作用域为class 作用域为module 作用域为session 返回值 参数化 @pytest.mark skipif跳过测试函数 xfail预期失败函数 parametrize参数化函数 单个参数 多个参数 conftest.py 在指定范围内共享fixture实例 scope="function" scope="module" 先实例化更大范围的设备 fixture的完成与拆卸(teardown)代码 1) yield方式 2) addfinalizer方式(推荐使用) yield和addfinalizer方式的区别: fixture参数化 fixture作用域 文件夹级别的覆盖 模块级别的覆盖 参数化覆盖 Allure 下载Allure 安装pytest-allure 生成报告 查看测试报告 allure特性 添加feature、story、step 添加allure.attach