mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-05 07:49:37 +08:00
add unit test and closes #971
This commit is contained in:
@@ -55,3 +55,24 @@ teststeps:
|
|||||||
- eq: ["body.form.foo1", "testcase_config_bar1"]
|
- eq: ["body.form.foo1", "testcase_config_bar1"]
|
||||||
- eq: ["body.form.foo2", "bar23"]
|
- eq: ["body.form.foo2", "bar23"]
|
||||||
- eq: ["body.form.foo3", "bar21"]
|
- eq: ["body.form.foo3", "bar21"]
|
||||||
|
|
||||||
|
-
|
||||||
|
name: post form data using json
|
||||||
|
variables:
|
||||||
|
foo2: bar23
|
||||||
|
jsondata:
|
||||||
|
foo1: $foo1
|
||||||
|
foo2: $foo2
|
||||||
|
foo3: $foo3
|
||||||
|
request:
|
||||||
|
method: POST
|
||||||
|
url: /post
|
||||||
|
headers:
|
||||||
|
User-Agent: HttpRunner/3.0
|
||||||
|
Content-Type: "application/json"
|
||||||
|
json: $jsondata
|
||||||
|
validate:
|
||||||
|
- eq: ["status_code", 200]
|
||||||
|
- eq: ["body.data.foo1", "testcase_config_bar1"]
|
||||||
|
- eq: ["body.data.foo2", "bar23"]
|
||||||
|
- eq: ["body.data.foo3", "bar21"]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# NOTE: Generated By HttpRunner v3.1.3
|
# NOTE: Generated By HttpRunner v3.1.3
|
||||||
# FROM: request_methods/request_with_variables.yml
|
# FROM: request_methods\request_with_variables.yml
|
||||||
|
|
||||||
|
|
||||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||||
@@ -62,6 +62,25 @@ class TestCaseRequestWithVariables(HttpRunner):
|
|||||||
.assert_equal("body.form.foo2", "bar23")
|
.assert_equal("body.form.foo2", "bar23")
|
||||||
.assert_equal("body.form.foo3", "bar21")
|
.assert_equal("body.form.foo3", "bar21")
|
||||||
),
|
),
|
||||||
|
Step(
|
||||||
|
RunRequest("post form data using json")
|
||||||
|
.with_variables(
|
||||||
|
**{
|
||||||
|
"foo2": "bar23",
|
||||||
|
"jsondata": {"foo1": "$foo1", "foo2": "$foo2", "foo3": "$foo3"},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.post("/post")
|
||||||
|
.with_headers(
|
||||||
|
**{"User-Agent": "HttpRunner/3.0", "Content-Type": "application/json"}
|
||||||
|
)
|
||||||
|
.with_json("$jsondata")
|
||||||
|
.validate()
|
||||||
|
.assert_equal("status_code", 200)
|
||||||
|
.assert_equal("body.data.foo1", "testcase_config_bar1")
|
||||||
|
.assert_equal("body.data.foo2", "bar23")
|
||||||
|
.assert_equal("body.data.foo3", "bar21")
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -237,9 +237,7 @@ def make_request_chain_style(request: Dict) -> Text:
|
|||||||
|
|
||||||
if "json" in request:
|
if "json" in request:
|
||||||
req_json = request["json"]
|
req_json = request["json"]
|
||||||
if not isinstance(req_json, Text):
|
if isinstance(req_json, Text):
|
||||||
pass
|
|
||||||
else:
|
|
||||||
req_json = f'"{req_json}"'
|
req_json = f'"{req_json}"'
|
||||||
request_chain_style += f".with_json({req_json})"
|
request_chain_style += f".with_json({req_json})"
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class TestLoader(unittest.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase_obj.config.name, "request methods testcase with variables"
|
testcase_obj.config.name, "request methods testcase with variables"
|
||||||
)
|
)
|
||||||
self.assertEqual(len(testcase_obj.teststeps), 3)
|
self.assertEqual(len(testcase_obj.teststeps), 4)
|
||||||
|
|
||||||
def test_load_json_file_file_format_error(self):
|
def test_load_json_file_file_format_error(self):
|
||||||
json_tmp_file = "tmp.json"
|
json_tmp_file = "tmp.json"
|
||||||
|
|||||||
@@ -214,3 +214,30 @@ from request_methods.request_with_functions_test import (
|
|||||||
teststep_chain_style,
|
teststep_chain_style,
|
||||||
"""Step(RunRequest("get with params").with_variables(**{'foo1': 'bar1', 'foo2': 123, 'sum_v': '${sum_two(1, 2)}'}).get("/get").with_params(**{'foo1': '$foo1', 'foo2': '$foo2', 'sum_v': '$sum_v'}).with_headers(**{'User-Agent': 'HttpRunner/${get_httprunner_version()}'}).extract().with_jmespath('body.args.foo1', 'session_foo1').with_jmespath('body.args.foo2', 'session_foo2').validate().assert_equal("status_code", 200).assert_equal("body.args.sum_v", "3"))""",
|
"""Step(RunRequest("get with params").with_variables(**{'foo1': 'bar1', 'foo2': 123, 'sum_v': '${sum_two(1, 2)}'}).get("/get").with_params(**{'foo1': '$foo1', 'foo2': '$foo2', 'sum_v': '$sum_v'}).with_headers(**{'User-Agent': 'HttpRunner/${get_httprunner_version()}'}).extract().with_jmespath('body.args.foo1', 'session_foo1').with_jmespath('body.args.foo2', 'session_foo2').validate().assert_equal("status_code", 200).assert_equal("body.args.sum_v", "3"))""",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_make_requests_with_json_chain_style(self):
|
||||||
|
step = {
|
||||||
|
"name": "get with params",
|
||||||
|
"variables": {"foo1": "bar1", "foo2": 123, "sum_v": "${sum_two(1, 2)}","myjson":{"name": "user", "password": "123456"}},
|
||||||
|
"request": {
|
||||||
|
"method": "GET",
|
||||||
|
"url": "/get",
|
||||||
|
"params": {"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"},
|
||||||
|
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
|
||||||
|
"json": "$myjson",
|
||||||
|
},
|
||||||
|
"testcase": "CLS_LB(TestCaseDemo)CLS_RB",
|
||||||
|
"extract": {
|
||||||
|
"session_foo1": "body.args.foo1",
|
||||||
|
"session_foo2": "body.args.foo2",
|
||||||
|
},
|
||||||
|
"validate": [
|
||||||
|
{"eq": ["status_code", 200]},
|
||||||
|
{"eq": ["body.args.sum_v", "3"]},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
teststep_chain_style = make_teststep_chain_style(step)
|
||||||
|
self.assertEqual(
|
||||||
|
teststep_chain_style,
|
||||||
|
"""Step(RunRequest("get with params").with_variables(**{'foo1': 'bar1', 'foo2': 123, 'sum_v': '${sum_two(1, 2)}', 'myjson': {'name': 'user', 'password': '123456'}}).get("/get").with_params(**{'foo1': '$foo1', 'foo2': '$foo2', 'sum_v': '$sum_v'}).with_headers(**{'User-Agent': 'HttpRunner/${get_httprunner_version()}'}).with_json("$myjson").extract().with_jmespath('body.args.foo1', 'session_foo1').with_jmespath('body.args.foo2', 'session_foo2').validate().assert_equal("status_code", 200).assert_equal("body.args.sum_v", "3"))""",
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user