前言环境要求 Python 大于等于3.8版本,(低于python3.8版本不支持) Pytest 7.2.0 最新版 v1.1.4 发布新增3个关键字 pip 安装插件, 最新版本v1.1.4 pip install pytest-yaml-yoyo
sleep 功能示例sleep 功能实现time.sleep() 等待时间,sleep参数可以是int类型和float 类型 get请求: name: GET请求示例 sleep: 5 request: method: GET url: http:///get validate: - eq: [status_code, 200]
sleep 也可以是一个变量,引用config设置的变量值 config: variables: x: 2.5 get请求: name: GET请求示例 sleep: ${x} request: method: GET url: http:///get validate: - eq: [status_code, 200]
sleep 执行顺序按写的顺序执行,如果放到request 前,那就在请求前先执行,放到request后,在请求后执行 skip 跳过用例pytest 实现跳过用例有2种方式 @pytest.mark.skip(reason="no way of currently testing this") def test_the_unknown(): ...
或者在用例里面跳过 import pytest if not pytest.config.getoption("--custom-flag"): pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)
本插件采用的第二种实现方式,在用例里面添加pytest.skip() 本插件 skip 关键字仅支持一个参数,那就是跳过的原因。使用示例 get请求: name: GET请求示例 skip: 功能缺失,暂时跳过此用例 request: method: GET url: http:///get validate: - eq: [status_code, 200]
运行用例可以看到 
如果用例是多个步骤组成的,也可以在步骤中跳过 teststeps: - name: step1-- request: method: GET url: http:///get validate: - eq: [status_code, 200]
- name: step2-- skip: 功能缺失,暂时跳过此用例 request: method: GET url: http:///get validate: - eq: [status_code, 200]
那么会按顺序执行,第一个步骤会执行,第二个步骤因为有skip,就跳过了 如果还有第三个步骤,那么一旦遇到skip ,整个用例就会结束,skip 跳过的是用例,而不是步骤! skipif 满足条件跳过skipif 后面参数是一个表达式,当表达式运行结果为真,那么就跳过用例 config: variables: x: 100
get请求: name: get skipif: ${x} > 50 request: method: GET url: http:///get validate: - eq: [status_code, 200]
skipif 还可以在多个用例中使用,当前面接口返回数据a,判断a满足条件就跳过后面用例 case1: name: get request: method: GET url: http:///get extract: xx: $.url validate: - eq: [status_code, 200]
case2: name: get skipif: "'org' in '${xx}'" request: method: GET url: http:///get validate: - eq: [status_code, 200]
skikif 后面的参数需是字符串表达式,通过eval()函数运行后得到结果,判断是否为真,为真的时候通过当前用例。 2023年第 13期《python接口web自动化+测试开发》课程,12月24号开学!
2022年第 2 期《Python 测试平台开发》进阶课程(10月30号开学)
|