此仅记录本人在pytest和allure的学习笔记
pytest命名规则
- 文件名:test_或_test开头或结尾
- 类:默认以Test开头普
- function:默认以test_开头
注:文件名不以test_或_test开头或结尾运行时会提示找不到文件
ERROR: file or directory not found: estlogincase.py
测试固件
测试固件 | 解释 |
---|---|
setup_module/teardown_module | 模块开始或结束运行一次 |
setup_function/teardown_function | 函数开始或结束运行一次 |
setup_class/teardown_class | 类开始或结束运行一次 |
setup_method/teardown_method | 方法开始或结束运行一次 |
setup/teardow | 开始或结束运行一次 |
注:当定义了类后使用setup_class/teardown_class ,setup_method/teardown_method ,不使用类时使用setup_module/teardown_module,setup_function/teardown_function,否则不会运行
pytest的执行
使用pytest.mian执行脚本
调用参数 | 解释 |
---|---|
pytest.main([‘-s’]) | 执行命令文件所在文件中所有复合条件的用例 |
pytest.main([‘-s’,‘test.py’]) | 仅执行test.py下的用例 |
pytest.main([‘-s’,‘test.py::Test01’]) | 执行test.py下Test 01类的用例 |
pytest.main([‘-s’,‘test.py’,‘–reruns 2’]) | 失败后重新运行2次 |
pytest.main([‘-s’,‘test.py’,‘–reruns 2’,‘–reruns-delay 2’]) | 失败后重新运行2次,每次等待2秒 |
@pytest.mark.L1 | 将用例标记为L1等级 |
pytest.main([‘-s’,‘test.py’,‘-m L1’]) | 仅执行标记为L1的用例 |
pytest.main([‘-s’,‘test.py’,‘-m L1 or L2’]) | 仅执行标记为L1或L2的用例 |
@pytest.skip(reason=“跳过原因”) | 无条件跳过 |
@pytest.skipif(跳过条件,reason=“跳过原因”) | 条件为true时,跳过 |
pytest参数化
基本格式:
data=[(‘admin’,‘pwd’,‘123’),((‘admin1’,‘pwd1’,‘1234’)]
类外使用
@pytest.mark.parametrize((‘参数对应函数名1’,‘参数对应函数名2’,‘参数对应函数名3’),data)
allure参数
参数 | 解释 |
---|---|
@allure.feature(“”) | 标记功能模块一般在类上使用 |
@allure.story(“”) | 标记功能模块一般在方法上使用 |
@allure.step(“”) | 标记测试步骤 |
@allure.severity(“”) | 用例重要级别:blocker致命,critical严重,normal一般(默认),minor次要,trivial轻微 |
allure 调用
pytest.main([‘-s’,‘test.py’,‘–allure’,‘./report’])
在report文件下生成allure报告的json数据
命令行输入allure generated --clean report 清除上次报告数据后生成html报告