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