mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-16 09:37:39 +08:00
change: override variables
(1) testcase: session variables > step variables > config variables (2) testsuite: testcase variables > config variables (3) testsuite testcase variables > testcase config variables
This commit is contained in:
@@ -9,5 +9,9 @@ def sum_two(m, n):
|
||||
return m + n
|
||||
|
||||
|
||||
def get_variables():
|
||||
return {"foo1": "session_bar1"}
|
||||
def get_testcase_config_variables():
|
||||
return {"foo1": "testcase_config_bar1", "foo2": "testcase_config_bar2"}
|
||||
|
||||
|
||||
def get_testsuite_config_variables():
|
||||
return {"foo1": "testsuite_config_bar1", "foo2": "testsuite_config_bar2"}
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
config:
|
||||
name: "demo testsuite"
|
||||
variables: ${get_variables()}
|
||||
variables: ${get_testsuite_config_variables()}
|
||||
|
||||
testcases:
|
||||
-
|
||||
name: request with functions
|
||||
testcase: request_methods/request_with_functions.yml
|
||||
variables:
|
||||
var1: testsuite_val1
|
||||
foo1: testcase_ref_bar11
|
||||
expect_foo1: testcase_ref_bar11
|
||||
expect_foo2: testsuite_config_bar2
|
||||
-
|
||||
name: request with referenced testcase
|
||||
testcase: request_methods/request_with_testcase_reference.yml
|
||||
variables:
|
||||
var2: testsuite_val2
|
||||
foo1: testcase_ref_bar12
|
||||
expect_foo1: testcase_ref_bar12
|
||||
foo2: testcase_ref_bar22
|
||||
expect_foo2: testcase_ref_bar22
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/request_with_functions.yml
|
||||
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
@@ -7,32 +7,39 @@ from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
class TestCaseRequestWithFunctions(HttpRunner):
|
||||
config = (
|
||||
Config("request with functions")
|
||||
.variables(**{"var1": "testsuite_val1", "foo1": "session_bar1"})
|
||||
.variables(
|
||||
**{
|
||||
"foo1": "testcase_ref_bar11",
|
||||
"foo2": "testsuite_config_bar2",
|
||||
"expect_foo1": "testcase_ref_bar11",
|
||||
"expect_foo2": "testsuite_config_bar2",
|
||||
}
|
||||
)
|
||||
.base_url("https://postman-echo.com")
|
||||
.verify(False)
|
||||
.export(*["session_foo2"])
|
||||
.export(*["foo3"])
|
||||
)
|
||||
|
||||
teststeps = [
|
||||
Step(
|
||||
RunRequest("get with params")
|
||||
.with_variables(
|
||||
**{"foo1": "bar1", "foo2": "session_bar2", "sum_v": "${sum_two(1, 2)}"}
|
||||
**{"foo1": "bar11", "foo2": "bar21", "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.foo2", "session_foo2")
|
||||
.with_jmespath("body.args.foo2", "foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.args.foo1", "session_bar1")
|
||||
.assert_equal("body.args.foo1", "bar11")
|
||||
.assert_equal("body.args.sum_v", "3")
|
||||
.assert_equal("body.args.foo2", "session_bar2")
|
||||
.assert_equal("body.args.foo2", "bar21")
|
||||
),
|
||||
Step(
|
||||
RunRequest("post raw text")
|
||||
.with_variables(**{"foo1": "hello world", "foo3": "$session_foo2"})
|
||||
.with_variables(**{"foo1": "bar12", "foo3": "bar32"})
|
||||
.post("/post")
|
||||
.with_headers(
|
||||
**{
|
||||
@@ -41,18 +48,18 @@ class TestCaseRequestWithFunctions(HttpRunner):
|
||||
}
|
||||
)
|
||||
.with_data(
|
||||
"This is expected to be sent back as part of response body: $foo1-$foo3."
|
||||
"This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
|
||||
)
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal(
|
||||
"body.data",
|
||||
"This is expected to be sent back as part of response body: session_bar1-session_bar2.",
|
||||
"This is expected to be sent back as part of response body: bar12-$expect_foo2-bar21.",
|
||||
)
|
||||
),
|
||||
Step(
|
||||
RunRequest("post form data")
|
||||
.with_variables(**{"foo1": "bar1", "foo2": "bar2"})
|
||||
.with_variables(**{"foo2": "bar23"})
|
||||
.post("/post")
|
||||
.with_headers(
|
||||
**{
|
||||
@@ -60,11 +67,12 @@ class TestCaseRequestWithFunctions(HttpRunner):
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
)
|
||||
.with_data("foo1=$foo1&foo2=$foo2")
|
||||
.with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.form.foo1", "session_bar1")
|
||||
.assert_equal("body.form.foo2", "bar2")
|
||||
.assert_equal("body.form.foo1", "$expect_foo1")
|
||||
.assert_equal("body.form.foo2", "bar23")
|
||||
.assert_equal("body.form.foo3", "bar21")
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml
|
||||
|
||||
import os
|
||||
@@ -16,7 +16,14 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
|
||||
class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
config = (
|
||||
Config("request with referenced testcase")
|
||||
.variables(**{"var2": "testsuite_val2", "foo1": "session_bar1"})
|
||||
.variables(
|
||||
**{
|
||||
"foo1": "testcase_ref_bar12",
|
||||
"expect_foo1": "testcase_ref_bar12",
|
||||
"expect_foo2": "testcase_ref_bar22",
|
||||
"foo2": "testcase_ref_bar22",
|
||||
}
|
||||
)
|
||||
.base_url("https://postman-echo.com")
|
||||
.verify(False)
|
||||
)
|
||||
@@ -24,9 +31,11 @@ class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
teststeps = [
|
||||
Step(
|
||||
RunTestCase("request with functions")
|
||||
.with_variables(**{"foo1": "override_bar1"})
|
||||
.with_variables(
|
||||
**{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"}
|
||||
)
|
||||
.call(RequestWithFunctions)
|
||||
.export(*["session_foo2"])
|
||||
.export(*["foo3"])
|
||||
),
|
||||
Step(
|
||||
RunRequest("post form data")
|
||||
@@ -38,11 +47,11 @@ class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
)
|
||||
.with_data("foo1=$foo1&foo2=$session_foo2")
|
||||
.with_data("foo1=$foo1&foo2=$foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.form.foo1", "session_bar1")
|
||||
.assert_equal("body.form.foo2", "session_bar2")
|
||||
.assert_equal("body.form.foo1", "bar1")
|
||||
.assert_equal("body.form.foo2", "bar21")
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/hardcode.yml
|
||||
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
config:
|
||||
name: "request methods testcase with functions"
|
||||
variables:
|
||||
foo1: session_bar1
|
||||
foo1: config_bar1
|
||||
foo2: config_bar2
|
||||
expect_foo1: config_bar1
|
||||
expect_foo2: config_bar2
|
||||
base_url: "https://postman-echo.com"
|
||||
verify: False
|
||||
export: ["session_foo2"]
|
||||
export: ["foo3"]
|
||||
|
||||
teststeps:
|
||||
-
|
||||
name: get with params
|
||||
variables:
|
||||
foo1: bar1
|
||||
foo2: session_bar2
|
||||
foo1: bar11
|
||||
foo2: bar21
|
||||
sum_v: "${sum_two(1, 2)}"
|
||||
request:
|
||||
method: GET
|
||||
@@ -23,40 +26,40 @@ teststeps:
|
||||
headers:
|
||||
User-Agent: HttpRunner/${get_httprunner_version()}
|
||||
extract:
|
||||
session_foo2: "body.args.foo2"
|
||||
foo3: "body.args.foo2"
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.args.foo1", "session_bar1"]
|
||||
- eq: ["body.args.foo1", "bar11"]
|
||||
- eq: ["body.args.sum_v", "3"]
|
||||
- eq: ["body.args.foo2", "session_bar2"]
|
||||
- eq: ["body.args.foo2", "bar21"]
|
||||
-
|
||||
name: post raw text
|
||||
variables:
|
||||
foo1: "hello world"
|
||||
foo3: "$session_foo2"
|
||||
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-$foo3."
|
||||
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: session_bar1-session_bar2."]
|
||||
- eq: ["body.data", "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar21."]
|
||||
-
|
||||
name: post form data
|
||||
variables:
|
||||
foo1: bar1
|
||||
foo2: bar2
|
||||
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"
|
||||
data: "foo1=$foo1&foo2=$foo2&foo3=$foo3"
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.form.foo1", "session_bar1"]
|
||||
- eq: ["body.form.foo2", "bar2"]
|
||||
- eq: ["body.form.foo1", "$expect_foo1"]
|
||||
- eq: ["body.form.foo2", "bar23"]
|
||||
- eq: ["body.form.foo3", "bar21"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/request_with_functions.yml
|
||||
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
@@ -7,32 +7,39 @@ from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
class TestCaseRequestWithFunctions(HttpRunner):
|
||||
config = (
|
||||
Config("request methods testcase with functions")
|
||||
.variables(**{"foo1": "session_bar1"})
|
||||
.variables(
|
||||
**{
|
||||
"foo1": "config_bar1",
|
||||
"foo2": "config_bar2",
|
||||
"expect_foo1": "config_bar1",
|
||||
"expect_foo2": "config_bar2",
|
||||
}
|
||||
)
|
||||
.base_url("https://postman-echo.com")
|
||||
.verify(False)
|
||||
.export(*["session_foo2"])
|
||||
.export(*["foo3"])
|
||||
)
|
||||
|
||||
teststeps = [
|
||||
Step(
|
||||
RunRequest("get with params")
|
||||
.with_variables(
|
||||
**{"foo1": "bar1", "foo2": "session_bar2", "sum_v": "${sum_two(1, 2)}"}
|
||||
**{"foo1": "bar11", "foo2": "bar21", "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.foo2", "session_foo2")
|
||||
.with_jmespath("body.args.foo2", "foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.args.foo1", "session_bar1")
|
||||
.assert_equal("body.args.foo1", "bar11")
|
||||
.assert_equal("body.args.sum_v", "3")
|
||||
.assert_equal("body.args.foo2", "session_bar2")
|
||||
.assert_equal("body.args.foo2", "bar21")
|
||||
),
|
||||
Step(
|
||||
RunRequest("post raw text")
|
||||
.with_variables(**{"foo1": "hello world", "foo3": "$session_foo2"})
|
||||
.with_variables(**{"foo1": "bar12", "foo3": "bar32"})
|
||||
.post("/post")
|
||||
.with_headers(
|
||||
**{
|
||||
@@ -41,18 +48,18 @@ class TestCaseRequestWithFunctions(HttpRunner):
|
||||
}
|
||||
)
|
||||
.with_data(
|
||||
"This is expected to be sent back as part of response body: $foo1-$foo3."
|
||||
"This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
|
||||
)
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal(
|
||||
"body.data",
|
||||
"This is expected to be sent back as part of response body: session_bar1-session_bar2.",
|
||||
"This is expected to be sent back as part of response body: bar12-$expect_foo2-bar21.",
|
||||
)
|
||||
),
|
||||
Step(
|
||||
RunRequest("post form data")
|
||||
.with_variables(**{"foo1": "bar1", "foo2": "bar2"})
|
||||
.with_variables(**{"foo2": "bar23"})
|
||||
.post("/post")
|
||||
.with_headers(
|
||||
**{
|
||||
@@ -60,11 +67,12 @@ class TestCaseRequestWithFunctions(HttpRunner):
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
)
|
||||
.with_data("foo1=$foo1&foo2=$foo2")
|
||||
.with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.form.foo1", "session_bar1")
|
||||
.assert_equal("body.form.foo2", "bar2")
|
||||
.assert_equal("body.form.foo1", "$expect_foo1")
|
||||
.assert_equal("body.form.foo2", "bar23")
|
||||
.assert_equal("body.form.foo3", "bar21")
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
config:
|
||||
name: "request methods testcase: reference testcase"
|
||||
variables:
|
||||
foo1: session_bar1
|
||||
foo1: testsuite_config_bar1
|
||||
expect_foo1: testsuite_config_bar1
|
||||
expect_foo2: config_bar2
|
||||
base_url: "https://postman-echo.com"
|
||||
verify: False
|
||||
|
||||
@@ -9,10 +11,11 @@ teststeps:
|
||||
-
|
||||
name: request with functions
|
||||
variables:
|
||||
foo1: override_bar1
|
||||
foo1: testcase_ref_bar1
|
||||
expect_foo1: testcase_ref_bar1
|
||||
testcase: request_methods/request_with_functions.yml
|
||||
export:
|
||||
- session_foo2
|
||||
- foo3
|
||||
-
|
||||
name: post form data
|
||||
variables:
|
||||
@@ -23,8 +26,8 @@ teststeps:
|
||||
headers:
|
||||
User-Agent: HttpRunner/${get_httprunner_version()}
|
||||
Content-Type: "application/x-www-form-urlencoded"
|
||||
data: "foo1=$foo1&foo2=$session_foo2"
|
||||
data: "foo1=$foo1&foo2=$foo3"
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.form.foo1", "session_bar1"]
|
||||
- eq: ["body.form.foo2", "session_bar2"]
|
||||
- eq: ["body.form.foo1", "bar1"]
|
||||
- eq: ["body.form.foo2", "bar21"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml
|
||||
|
||||
import os
|
||||
@@ -16,7 +16,13 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
|
||||
class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
config = (
|
||||
Config("request methods testcase: reference testcase")
|
||||
.variables(**{"foo1": "session_bar1"})
|
||||
.variables(
|
||||
**{
|
||||
"foo1": "testsuite_config_bar1",
|
||||
"expect_foo1": "testsuite_config_bar1",
|
||||
"expect_foo2": "config_bar2",
|
||||
}
|
||||
)
|
||||
.base_url("https://postman-echo.com")
|
||||
.verify(False)
|
||||
)
|
||||
@@ -24,9 +30,11 @@ class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
teststeps = [
|
||||
Step(
|
||||
RunTestCase("request with functions")
|
||||
.with_variables(**{"foo1": "override_bar1"})
|
||||
.with_variables(
|
||||
**{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"}
|
||||
)
|
||||
.call(RequestWithFunctions)
|
||||
.export(*["session_foo2"])
|
||||
.export(*["foo3"])
|
||||
),
|
||||
Step(
|
||||
RunRequest("post form data")
|
||||
@@ -38,11 +46,11 @@ class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
)
|
||||
.with_data("foo1=$foo1&foo2=$session_foo2")
|
||||
.with_data("foo1=$foo1&foo2=$foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.form.foo1", "session_bar1")
|
||||
.assert_equal("body.form.foo2", "session_bar2")
|
||||
.assert_equal("body.form.foo1", "bar1")
|
||||
.assert_equal("body.form.foo2", "bar21")
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
config:
|
||||
name: "request methods testcase with variables"
|
||||
variables: ${get_variables()}
|
||||
variables: ${get_testcase_config_variables()}
|
||||
base_url: "https://postman-echo.com"
|
||||
verify: False
|
||||
|
||||
@@ -8,8 +8,8 @@ teststeps:
|
||||
-
|
||||
name: get with params
|
||||
variables:
|
||||
foo1: bar1
|
||||
foo2: session_bar2
|
||||
foo1: bar11
|
||||
foo2: bar21
|
||||
request:
|
||||
method: GET
|
||||
url: /get
|
||||
@@ -19,39 +19,39 @@ teststeps:
|
||||
headers:
|
||||
User-Agent: HttpRunner/3.0
|
||||
extract:
|
||||
session_foo2: "body.args.foo2"
|
||||
foo3: "body.args.foo2"
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.args.foo1", "session_bar1"]
|
||||
- eq: ["body.args.foo2", "session_bar2"]
|
||||
- eq: ["body.args.foo1", "bar11"]
|
||||
- eq: ["body.args.foo2", "bar21"]
|
||||
-
|
||||
name: post raw text
|
||||
variables:
|
||||
foo1: "hello world"
|
||||
foo3: "$session_foo2"
|
||||
foo1: "bar12"
|
||||
foo3: "bar32"
|
||||
request:
|
||||
method: POST
|
||||
url: /post
|
||||
headers:
|
||||
User-Agent: HttpRunner/3.0
|
||||
Content-Type: "text/plain"
|
||||
data: "This is expected to be sent back as part of response body: $foo1-$foo3."
|
||||
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: session_bar1-session_bar2."]
|
||||
- eq: ["body.data", "This is expected to be sent back as part of response body: bar12-testcase_config_bar2-bar21."]
|
||||
-
|
||||
name: post form data
|
||||
variables:
|
||||
foo1: bar1
|
||||
foo2: bar2
|
||||
foo2: bar23
|
||||
request:
|
||||
method: POST
|
||||
url: /post
|
||||
headers:
|
||||
User-Agent: HttpRunner/3.0
|
||||
Content-Type: "application/x-www-form-urlencoded"
|
||||
data: "foo1=$foo1&foo2=$foo2"
|
||||
data: "foo1=$foo1&foo2=$foo2&foo3=$foo3"
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.form.foo1", "session_bar1"]
|
||||
- eq: ["body.form.foo2", "bar2"]
|
||||
- eq: ["body.form.foo1", "testcase_config_bar1"]
|
||||
- eq: ["body.form.foo2", "bar23"]
|
||||
- eq: ["body.form.foo3", "bar21"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/request_with_variables.yml
|
||||
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
@@ -7,7 +7,7 @@ from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
class TestCaseRequestWithVariables(HttpRunner):
|
||||
config = (
|
||||
Config("request methods testcase with variables")
|
||||
.variables(**{"foo1": "session_bar1"})
|
||||
.variables(**{"foo1": "testcase_config_bar1", "foo2": "testcase_config_bar2"})
|
||||
.base_url("https://postman-echo.com")
|
||||
.verify(False)
|
||||
)
|
||||
@@ -15,37 +15,37 @@ class TestCaseRequestWithVariables(HttpRunner):
|
||||
teststeps = [
|
||||
Step(
|
||||
RunRequest("get with params")
|
||||
.with_variables(**{"foo1": "bar1", "foo2": "session_bar2"})
|
||||
.with_variables(**{"foo1": "bar11", "foo2": "bar21"})
|
||||
.get("/get")
|
||||
.with_params(**{"foo1": "$foo1", "foo2": "$foo2"})
|
||||
.with_headers(**{"User-Agent": "HttpRunner/3.0"})
|
||||
.extract()
|
||||
.with_jmespath("body.args.foo2", "session_foo2")
|
||||
.with_jmespath("body.args.foo2", "foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.args.foo1", "session_bar1")
|
||||
.assert_equal("body.args.foo2", "session_bar2")
|
||||
.assert_equal("body.args.foo1", "bar11")
|
||||
.assert_equal("body.args.foo2", "bar21")
|
||||
),
|
||||
Step(
|
||||
RunRequest("post raw text")
|
||||
.with_variables(**{"foo1": "hello world", "foo3": "$session_foo2"})
|
||||
.with_variables(**{"foo1": "bar12", "foo3": "bar32"})
|
||||
.post("/post")
|
||||
.with_headers(
|
||||
**{"User-Agent": "HttpRunner/3.0", "Content-Type": "text/plain"}
|
||||
)
|
||||
.with_data(
|
||||
"This is expected to be sent back as part of response body: $foo1-$foo3."
|
||||
"This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
|
||||
)
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal(
|
||||
"body.data",
|
||||
"This is expected to be sent back as part of response body: session_bar1-session_bar2.",
|
||||
"This is expected to be sent back as part of response body: bar12-testcase_config_bar2-bar21.",
|
||||
)
|
||||
),
|
||||
Step(
|
||||
RunRequest("post form data")
|
||||
.with_variables(**{"foo1": "bar1", "foo2": "bar2"})
|
||||
.with_variables(**{"foo2": "bar23"})
|
||||
.post("/post")
|
||||
.with_headers(
|
||||
**{
|
||||
@@ -53,11 +53,12 @@ class TestCaseRequestWithVariables(HttpRunner):
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
)
|
||||
.with_data("foo1=$foo1&foo2=$foo2")
|
||||
.with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
|
||||
.validate()
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal("body.form.foo1", "session_bar1")
|
||||
.assert_equal("body.form.foo2", "bar2")
|
||||
.assert_equal("body.form.foo1", "testcase_config_bar1")
|
||||
.assert_equal("body.form.foo2", "bar23")
|
||||
.assert_equal("body.form.foo3", "bar21")
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -26,4 +26,4 @@ teststeps:
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.args.sum_v", "3"]
|
||||
# - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] TODO
|
||||
# - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] FIXME: TypeError: '<' not supported between instances of 'str' and 'int'
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/validate_with_functions.yml
|
||||
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
|
||||
@@ -39,7 +39,7 @@ teststeps:
|
||||
data: "This is expected to be sent back as part of response body: $foo1-$foo3."
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.data", "This is expected to be sent back as part of response body: session_bar1-$foo3."]
|
||||
- eq: ["body.data", "This is expected to be sent back as part of response body: hello world-$foo3."]
|
||||
-
|
||||
name: post form data
|
||||
variables:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# NOTE: Generated By HttpRunner v3.0.10
|
||||
# NOTE: Generated By HttpRunner v3.0.11
|
||||
# FROM: examples/postman_echo/request_methods/validate_with_variables.yml
|
||||
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
@@ -40,7 +40,7 @@ class TestCaseValidateWithVariables(HttpRunner):
|
||||
.assert_equal("status_code", 200)
|
||||
.assert_equal(
|
||||
"body.data",
|
||||
"This is expected to be sent back as part of response body: session_bar1-$foo3.",
|
||||
"This is expected to be sent back as part of response body: hello world-$foo3.",
|
||||
)
|
||||
),
|
||||
Step(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
__version__ = "3.0.10"
|
||||
__version__ = "3.0.11"
|
||||
__description__ = "One-stop solution for HTTP(S) testing."
|
||||
|
||||
from httprunner.runner import HttpRunner
|
||||
|
||||
@@ -21,9 +21,8 @@ from httprunner.loader import (
|
||||
load_testsuite,
|
||||
load_project_meta,
|
||||
)
|
||||
from httprunner.parser import parse_data
|
||||
from httprunner.response import uniform_validator
|
||||
from httprunner.utils import ensure_file_path_valid
|
||||
from httprunner.utils import ensure_file_path_valid, override_config_variables
|
||||
|
||||
""" cache converted pytest files, avoid duplicate making
|
||||
"""
|
||||
@@ -407,11 +406,13 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
|
||||
if "verify" in testsuite_config:
|
||||
testcase_dict["config"]["verify"] = testsuite_config["verify"]
|
||||
# override variables
|
||||
# testsuite config variables > testsuite testcase variables
|
||||
# testsuite testcase variables > testsuite config variables
|
||||
testcase_variables = convert_variables(
|
||||
testcase.get("variables", {}), testcase_path
|
||||
)
|
||||
testcase_variables.update(testsuite_variables)
|
||||
testcase_variables = override_config_variables(
|
||||
testcase_variables, testsuite_variables
|
||||
)
|
||||
# testsuite testcase variables > testcase config variables
|
||||
testcase_dict["config"]["variables"] = convert_variables(
|
||||
testcase_dict["config"].get("variables", {}), testcase_path
|
||||
|
||||
@@ -21,6 +21,7 @@ from httprunner.loader import load_project_meta, load_testcase_file
|
||||
from httprunner.parser import build_url, parse_data, parse_variables_mapping
|
||||
from httprunner.response import ResponseObject
|
||||
from httprunner.testcase import Config, Step
|
||||
from httprunner.utils import override_config_variables
|
||||
from httprunner.models import (
|
||||
TConfig,
|
||||
TStep,
|
||||
@@ -324,20 +325,26 @@ class HttpRunner(object):
|
||||
|
||||
# run teststeps
|
||||
for step in self.__teststeps:
|
||||
# update with config variables
|
||||
step.variables.update(self.__config.variables)
|
||||
# update with session variables extracted from pre step
|
||||
# override variables
|
||||
# session variables (extracted from pre step) > step variables
|
||||
step.variables.update(self.__session_variables)
|
||||
# step variables > testcase config variables
|
||||
step.variables = override_config_variables(
|
||||
step.variables, self.__config.variables
|
||||
)
|
||||
|
||||
# parse variables
|
||||
step.variables = parse_variables_mapping(
|
||||
step.variables, self.__project_meta.functions
|
||||
)
|
||||
|
||||
# run step
|
||||
if USE_ALLURE:
|
||||
with allure.step(f"step: {step.name}"):
|
||||
extract_mapping = self.__run_step(step)
|
||||
else:
|
||||
extract_mapping = self.__run_step(step)
|
||||
|
||||
# save extracted variables to session variables
|
||||
self.__session_variables.update(extract_mapping)
|
||||
|
||||
@@ -413,10 +420,10 @@ class HttpRunner(object):
|
||||
log_handler = logger.add(self.__log_path, level="DEBUG")
|
||||
|
||||
# parse config name
|
||||
variables = self.__config.variables
|
||||
variables.update(self.__session_variables)
|
||||
config_variables = self.__config.variables
|
||||
config_variables.update(self.__session_variables)
|
||||
self.__config.name = parse_data(
|
||||
self.__config.name, variables, self.__project_meta.functions
|
||||
self.__config.name, config_variables, self.__project_meta.functions
|
||||
)
|
||||
|
||||
if USE_ALLURE:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import collections
|
||||
import copy
|
||||
import json
|
||||
import os.path
|
||||
import platform
|
||||
@@ -11,6 +12,7 @@ from loguru import logger
|
||||
|
||||
from httprunner import __version__
|
||||
from httprunner import exceptions
|
||||
from httprunner.models import VariablesMapping
|
||||
|
||||
|
||||
def init_sentry_sdk():
|
||||
@@ -223,3 +225,23 @@ class ExtendJSONEncoder(json.JSONEncoder):
|
||||
return super(ExtendJSONEncoder, self).default(obj)
|
||||
except (UnicodeDecodeError, TypeError):
|
||||
return repr(obj)
|
||||
|
||||
|
||||
def override_config_variables(
|
||||
step_variables: VariablesMapping, config_variables: VariablesMapping
|
||||
) -> VariablesMapping:
|
||||
""" override variables:
|
||||
testcase step variables > testcase config variables
|
||||
testsuite testcase variables > testsuite config variables
|
||||
"""
|
||||
step_new_variables = {}
|
||||
for key, value in step_variables.items():
|
||||
if f"${key}" == value:
|
||||
# e.g. {"base_url": "$base_url"}
|
||||
continue
|
||||
|
||||
step_new_variables[key] = value
|
||||
|
||||
variables = copy.deepcopy(config_variables)
|
||||
variables.update(step_new_variables)
|
||||
return variables
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "httprunner"
|
||||
version = "3.0.10"
|
||||
version = "3.0.11"
|
||||
description = "One-stop solution for HTTP(S) testing."
|
||||
license = "Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
||||
@@ -4,7 +4,11 @@ import os
|
||||
import unittest
|
||||
|
||||
from httprunner import loader, utils
|
||||
from httprunner.utils import ensure_file_path_valid, ExtendJSONEncoder
|
||||
from httprunner.utils import (
|
||||
ensure_file_path_valid,
|
||||
ExtendJSONEncoder,
|
||||
override_config_variables,
|
||||
)
|
||||
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
@@ -129,3 +133,11 @@ class TestUtils(unittest.TestCase):
|
||||
json.dumps(data)
|
||||
|
||||
json.dumps(data, cls=ExtendJSONEncoder)
|
||||
|
||||
def test_override_config_variables(self):
|
||||
step_variables = {"base_url": "$base_url", "foo1": "bar1"}
|
||||
config_variables = {"base_url": "https://httpbin.org", "foo1": "bar111"}
|
||||
self.assertEqual(
|
||||
override_config_variables(step_variables, config_variables),
|
||||
{"base_url": "https://httpbin.org", "foo1": "bar111"},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user