1 # NOTE: Generated By HttpRunner v3.1.6
2 # FROM: testcases\demo_testcase_request.yml
3
4
5 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
6
7
8 class TestCaseDemoTestcaseRequest(HttpRunner):
9 # 用例配置:全局变量,可以在其中进行测试用例级别的设置(将每个测试用例视为一个黑盒,配置variables是输入部分,而配置export是输出部分)
10 config = (
11 Config("接口测试用例的名称")
12 # 变量:局部变量大于全局变量(步骤里面配置的变量比此处配置的变量具有更高的优先级)
13 .variables(
14 **{
15 "foo1": "config_bar1",
16 "foo2": "config_bar2",
17 "expect_foo1": "config_bar1",
18 "expect_foo2": "config_bar2",
19 }
20 )
21 # base_url:全局变量,如果此处设置了,则测试用例步骤里只能是相对路径,不能写全
22 .base_url("https://postman-echo.com")
23 # 是否验证服务器的TLS证书,除非你想测试用例执行的HTTP流量,默认是false
24 .verify(False)
25 # 关联接口之间,变量的提取(当一个测试用例在另一个测试用例的步骤中被引用,并且将被提取一些会话变量以在随后的测试步骤中使用时,则所提取的会话变量应在config export部分中进行配置)
26 .export(*["foo3"])
27 # 性能的并发数
28 .locust_weight(2)
29 )
30 # 测试用例的步骤:每一个步骤对应于一个API请求或者对另一个测试用例引用调用
31 teststeps = [
32 Step(
33 # 在一个步骤中使用“ RunRequest”来向API发出请求,并对响应进行一些提取或验证
34 RunRequest("该测试用例下步骤1的名字")
35 # 该请求的变量:每个步骤的变量都是独立的,因此,如果要在多个步骤中共享变量,则应在配置变量中定义变量;此外,步骤变量将覆盖配置变量中具有相同名称的变量
36 .with_variables(
37 **{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two(1, 2)}"}
38 )
39 # 请求的url,指相对路径
40 .get("/get")
41 # 请求的参数:Params参数
42 .with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
43 # 请求的Headers信息
44 .with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
45 # 请求的cookies
46 .with_cookies("xx")
47 # 请求HTTP的正文,data参数
48 .with_data("xxx")
49 # 在json中指定HTTP请求正文
50 .with_json("xxx")
51 # 提取JSON响应主体
52 .extract()
53 .with_jmespath("body.args.foo2", "foo3")
54
55 # 页面加载后对表单进行验证
56 .validate()
57 # 判断响应码是不是200
58 .assert_equal("status_code", 200)
59 # 判断字段foo1的值是不是bar11
60 .assert_equal("body.args.foo1", "bar11")
61 .assert_equal("body.args.sum_v", "3")
62 .assert_equal("body.args.foo2", "bar21")
63 ),
64 Step(
65 RunRequest("该测试用例下步骤2的名字")
66 .with_variables(**{"foo1": "bar12", "foo3": "bar32"})
67 .post("/post")
68 .with_headers(
69 **{
70 "User-Agent": "HttpRunner/${get_httprunner_version()}",
71 "Content-Type": "text/plain",
72 }
73 )
74 .with_data(
75 "This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
76 )
77 .validate()
78 .assert_equal("status_code", 200)
79 .assert_equal(
80 "body.data",
81 "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32.",
82 )
83 ),
84 Step(
85 RunRequest("该测试用例下步骤3的名字")
86 .with_variables(**{"foo2": "bar23"})
87 .post("/post")
88 .with_headers(
89 **{
90 "User-Agent": "HttpRunner/${get_httprunner_version()}",
91 "Content-Type": "application/x-www-form-urlencoded",
92 }
93 )
94 .with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
95 .validate()
96 .assert_equal("status_code", 200)
97 .assert_equal("body.form.foo1", "$expect_foo1")
98 .assert_equal("body.form.foo2", "bar23")
99 .assert_equal("body.form.foo3", "bar21")
100 ),
101 ]
102
103
104 if __name__ == "__main__":
105 TestCaseDemoTestcaseRequest().test_start()
# demo_banner_test.py
import pytest
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from testcases.pytest测试用例示例.test_001_banner import (
TestCaseTest001Banner as Banner001,
)
from testcases.pytest测试用例示例.test_003_banner import (
TestCaseTest003Banner as Banner003,
)
from httprunner import Parameters
from lib.common.config_operate import read_ini_config
@pytest.mark.skipif(
read_ini_config("Header", "accept-language") == "zh-CN", reason="仅适用于地球外的测试环境"
)
class TestCaseTest002Banner(HttpRunner):
# 变量_全局变量:字段取值参数化
@pytest.mark.parametrize(
"param",
Parameters(
{
"title": "${get_user_id(10)}",
"user": ["user1", "user2"],
"title-user": [("demo4", "4"), ("demo5", "5"), ("dmeo6", "6")],
}
),
)
def test_start(self, param):
super().test_start(param)
config = (
Config("开启和关闭banner成功")
# 变量_全局变量:这里可以替换需要配置的参数,当调用用例的时候可以使用 with_variables()重写参数的值。这里是这条测试用例的全局变量。优先级没有测试步骤里面的高
.variables(
**{
"xx": "xx",
"yy": "yy",
}
)
.base_url("${read_ini_config(Host, rem_host_001)}")
.verify(False)
# 变量_局部变量:该用例被调用时传递出去的变量,可供其他脚本使用
.export("code")
)
teststeps = [
# 测试步骤1
Step(
RunTestCase("前置1:添加一条banner数据")
.setup_hook("${setup_clean_data()}") # 调用用例前执行测试前置
# 变量_局部变量:更新某个 step 里面的字段值
.with_variables(
**{
"xx": "yy",
}
)
.call(Banner001) # 调用用例
.teardown_hook("${teardown_clean_data()}") # 测试后置
# 变量_局部变量:导出某个值,供下面的步骤使用。这个值来自于本条用例响应内容with_jmespath()
.export("xx")
),
Step(RunTestCase("前置2:获取banner页第一行数据ID").call(Banner003).export("id")),
Step(
RunRequest("步骤1:关闭 banner ")
.with_variables(**{"xx": "as"})
# hook_测试前:测试前的清理操作
.setup_hook("${setup_clean_data1()}")
.setup_hook("${setup_clean_data2()}")
.setup_hook("${setup_clean_data3()}")
.post(
"/xxx-app/xxx/operaxxx"
)
.with_headers(
**{
"accept-language": "${read_ini_config(Header, accept-language)}",
"autel-token": "${get_login_info(expert_001, token)}",
}
)
# 变量_引用:使用 ${id} 或者 $id,一般使用 ${id}
.with_json({"id": "$id", "displayStatus": False})
# hook_测试后:测试数据清理
.teardown_hook("${teardown_clean_data4()}")
.teardown_hook("${teardown_clean_data5()}")
# 变量_局部变量:提取响应的参数值
.extract()
.with_jmespath("body.code", "code")
# 校验
.validate()
.assert_equal('headers."Content-Type"', "application/json")
.assert_equal("status_code", 200)
.assert_equal("body.code", "200")
.assert_equal("body.message", "success")
.assert_contains("body.data.records[0].name", "接口自动化")
),
]
if __name__ == "__main__":
TestCaseTest002Banner().test_start()