3 - 单个测试用例场景:demo_testcase_request_test.py
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 # 变量:局部变量大于全局变量(teststeps步骤里面配置的局部变量 比 此处配置的全局变量 具有更高的优先级) 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 # 该请求的变量:每个步骤的变量都是独立的,因此,如果要在多个步骤中共享变量,则应在Config配置变量中定义变量;此外,步骤变量将覆盖配置变量中具有相同名称的变量 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_testcase_request.json: https://ontheway-cool.gitee.io/httprunner3doc_cn/concepts/demo_testcase_request.json

{ "config": { "name": "request methods testcase with functions", "variables": { "foo1": "config_bar1", "foo2": "config_bar2", "expect_foo1": "config_bar1", "expect_foo2": "config_bar2" }, "base_url": "https://postman-echo.com", "verify": false, "export": [ "foo3" ] }, "teststeps": [ { "name": "get with params", "variables": { "foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two(1, 2)}" }, "request": { "method": "GET", "url": "/get", "params": { "foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v" }, "headers": { "User-Agent": "HttpRunner/${get_httprunner_version()}" } }, "extract": { "foo3": "body.args.foo2" }, "validate": [ { "eq": [ "status_code", 200 ] }, { "eq": [ "body.args.foo1", "bar11" ] }, { "eq": [ "body.args.sum_v", "3" ] }, { "eq": [ "body.args.foo2", "bar21" ] } ] }, { "name": "post raw text", "variables": { "foo1": "bar12", "foo3": "bar32" }, "request": { "method": "POST", "url": "/post", "headers": { "User-Agent": "HttpRunner/${get_httprunner_version()}", "Content-Type": "text/plain" }, "data": "This is expected to be sent back as part of response body: $foo1-$foo2-$foo3." }, "validate": [ { "eq": [ "status_code", 200 ] }, { "eq": [ "body.data", "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32." ] } ] }, { "name": "post form data", "variables": { "foo2": "bar23" }, "request": { "method": "POST", "url": "/post", "headers": { "User-Agent": "HttpRunner/${get_httprunner_version()}", "Content-Type": "application/x-www-form-urlencoded" }, "data": "foo1=$foo1&foo2=$foo2&foo3=$foo3" }, "validate": [ { "eq": [ "status_code", 200 ] }, { "eq": [ "body.form.foo1", "$expect_foo1" ] }, { "eq": [ "body.form.foo2", "bar23" ] }, { "eq": [ "body.form.foo3", "bar21" ] } ] } ] }
demo_testcase_request.yml: https://ontheway-cool.gitee.io/httprunner3doc_cn/concepts/demo_testcase_request.yml

config: name: "request methods testcase with functions" variables: foo1: config_bar1 foo2: config_bar2 expect_foo1: config_bar1 expect_foo2: config_bar2 base_url: "https://postman-echo.com" verify: False export: ["foo3"] teststeps: - name: get with params variables: foo1: bar11 foo2: bar21 sum_v: "${sum_two(1, 2)}" request: method: GET url: /get params: foo1: $foo1 foo2: $foo2 sum_v: $sum_v headers: User-Agent: HttpRunner/${get_httprunner_version()} extract: foo3: "body.args.foo2" validate: - eq: ["status_code", 200] - eq: ["body.args.foo1", "bar11"] - eq: ["body.args.sum_v", "3"] - eq: ["body.args.foo2", "bar21"] - name: post raw text variables: foo1: "bar12" foo3: "bar32" request: method: POST url: /post headers: User-Agent: HttpRunner/${get_httprunner_version()} Content-Type: "text/plain" data: "This is expected to be sent back as part of response body: $foo1-$foo2-$foo3." validate: - eq: ["status_code", 200] - eq: ["body.data", "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32."] - name: post form data variables: foo2: bar23 request: method: POST url: /post headers: User-Agent: HttpRunner/${get_httprunner_version()} Content-Type: "application/x-www-form-urlencoded" data: "foo1=$foo1&foo2=$foo2&foo3=$foo3" validate: - eq: ["status_code", 200] - eq: ["body.form.foo1", "$expect_foo1"] - eq: ["body.form.foo2", "bar23"] - eq: ["body.form.foo3", "bar21"]