init: move from httprunner/httprunner

This commit is contained in:
lilong.129
2025-02-05 21:32:44 +08:00
commit f4860de5ad
104 changed files with 12602 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
# NOTE: Generated By hrp v4.2.0, DO NOT EDIT!
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from debugtalk import *
if __name__ == "__main__":
import funppy
funppy.register("get_httprunner_version", get_httprunner_version)
funppy.register("sum_two", sum_two)
funppy.register("get_testcase_config_variables", get_testcase_config_variables)
funppy.register("get_testsuite_config_variables", get_testsuite_config_variables)
funppy.register("get_app_version", get_app_version)
funppy.register("calculate_two_nums", calculate_two_nums)
funppy.register("fake_rand_count", fake_rand_count)
funppy.serve()
View File
+65
View File
@@ -0,0 +1,65 @@
# NOTICE: Generated By HttpRunner.
import json
import os
import time
import pytest
from loguru import logger
from httprunner.utils import get_platform, ExtendJSONEncoder
@pytest.fixture(scope="session", autouse=True)
def session_fixture(request):
"""setup and teardown each task"""
logger.info("start running testcases ...")
start_at = time.time()
yield
logger.info("task finished, generate task summary for --save-tests")
summary = {
"success": True,
"stat": {
"testcases": {"total": 0, "success": 0, "fail": 0},
"teststeps": {"total": 0, "failures": 0, "successes": 0},
},
"time": {"start_at": start_at, "duration": time.time() - start_at},
"platform": get_platform(),
"details": [],
}
for item in request.node.items:
testcase_summary = item.instance.get_summary()
summary["success"] &= testcase_summary.success
summary["stat"]["testcases"]["total"] += 1
summary["stat"]["teststeps"]["total"] += len(testcase_summary.step_results)
if testcase_summary.success:
summary["stat"]["testcases"]["success"] += 1
summary["stat"]["teststeps"]["successes"] += len(
testcase_summary.step_results
)
else:
summary["stat"]["testcases"]["fail"] += 1
summary["stat"]["teststeps"]["successes"] += (
len(testcase_summary.step_results) - 1
)
summary["stat"]["teststeps"]["failures"] += 1
testcase_summary_json = testcase_summary.dict()
testcase_summary_json["records"] = testcase_summary_json.pop("step_results")
summary["details"].append(testcase_summary_json)
summary_path = os.path.join(
os.getcwd(), "examples/postman_echo/logs/request_methods/hardcode.summary.json"
)
summary_dir = os.path.dirname(summary_path)
os.makedirs(summary_dir, exist_ok=True)
with open(summary_path, "w", encoding="utf-8") as f:
json.dump(summary, f, indent=4, ensure_ascii=False, cls=ExtendJSONEncoder)
logger.info(f"generated task summary: {summary_path}")
@@ -0,0 +1 @@
# NOTICE: Generated By HttpRunner. DO NOT EDIT!
@@ -0,0 +1,34 @@
config:
name: "set & delete cookies."
base_url: "https://postman-echo.com"
verify: False
export: ["cookie_foo1"]
teststeps:
-
name: set cookie foo1 & foo2 & foo3
request:
method: GET
url: /cookies/set
params:
foo1: bar1
foo2: bar2
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
extract:
cookie_foo1: body.cookies.foo1
validate:
- eq: ["status_code", 200]
- eq: ["body.cookies.foo1", "bar1"]
- eq: ["body.cookies.foo2", "bar2"]
-
name: delete cookie foo2
request:
method: GET
url: /cookies/delete?foo2
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
validate:
- eq: ["status_code", 200]
- eq: ["body.cookies.foo1", "bar1"]
- eq: ["body.cookies.foo2", null]
@@ -0,0 +1,41 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: cookie_manipulation/hardcode.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseHardcode(HttpRunner):
config = (
Config("set & delete cookies.")
.base_url("https://postman-echo.com")
.verify(False)
.export(*["cookie_foo1"])
)
teststeps = [
Step(
RunRequest("set cookie foo1 & foo2 & foo3")
.get("/cookies/set")
.with_params(**{"foo1": "bar1", "foo2": "bar2"})
.with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
.extract()
.with_jmespath("body.cookies.foo1", "cookie_foo1")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.cookies.foo1", "bar1")
.assert_equal("body.cookies.foo2", "bar2")
),
Step(
RunRequest("delete cookie foo2")
.get("/cookies/delete?foo2")
.with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.cookies.foo1", "bar1")
.assert_equal("body.cookies.foo2", None)
),
]
if __name__ == "__main__":
TestCaseHardcode().test_start()
@@ -0,0 +1,41 @@
config:
name: "set & delete cookies."
variables:
foo1: bar1
foo2: bar2
base_url: "https://postman-echo.com"
verify: False
export: ["cookie_foo1", "cookie_foo3"]
teststeps:
-
name: set cookie foo1 & foo2 & foo3
variables:
foo3: bar3
request:
method: GET
url: /cookies/set
params:
foo1: bar111
foo2: $foo2
foo3: $foo3
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
extract:
cookie_foo1: $.cookies.foo1
cookie_foo3: $.cookies.foo3
validate:
- eq: ["status_code", 200]
- ne: ["$.cookies.foo3", "$foo3"]
-
name: delete cookie foo2
request:
method: GET
url: /cookies/delete?foo2
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
validate:
- eq: ["status_code", 200]
- ne: ["$.cookies.foo1", "$foo1"]
- eq: ["$.cookies.foo1", "$cookie_foo1"]
- eq: ["$.cookies.foo3", "$cookie_foo3"]
@@ -0,0 +1,44 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: cookie_manipulation/set_delete_cookies.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseSetDeleteCookies(HttpRunner):
config = (
Config("set & delete cookies.")
.variables(**{"foo1": "bar1", "foo2": "bar2"})
.base_url("https://postman-echo.com")
.verify(False)
.export(*["cookie_foo1", "cookie_foo3"])
)
teststeps = [
Step(
RunRequest("set cookie foo1 & foo2 & foo3")
.with_variables(**{"foo3": "bar3"})
.get("/cookies/set")
.with_params(**{"foo1": "bar111", "foo2": "$foo2", "foo3": "$foo3"})
.with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
.extract()
.with_jmespath("$.cookies.foo1", "cookie_foo1")
.with_jmespath("$.cookies.foo3", "cookie_foo3")
.validate()
.assert_equal("status_code", 200)
.assert_not_equal("$.cookies.foo3", "$foo3")
),
Step(
RunRequest("delete cookie foo2")
.get("/cookies/delete?foo2")
.with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
.validate()
.assert_equal("status_code", 200)
.assert_not_equal("$.cookies.foo1", "$foo1")
.assert_equal("$.cookies.foo1", "$cookie_foo1")
.assert_equal("$.cookies.foo3", "$cookie_foo3")
),
]
if __name__ == "__main__":
TestCaseSetDeleteCookies().test_start()
+42
View File
@@ -0,0 +1,42 @@
from httprunner import __version__
def get_httprunner_version():
return __version__
def sum_two(m, n):
return m + n
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"}
def get_app_version():
return [3.1, 3.0]
def calculate_two_nums(a, b=1):
return [a + b, b - a]
def fake_rand_count():
"""
return 1 at first call
return 2 at second call
"""
l = []
def func():
l.append(1)
return len(l)
return func
fake_randnum = fake_rand_count()
@@ -0,0 +1 @@
# NOTICE: Generated By HttpRunner. DO NOT EDIT!
@@ -0,0 +1,4 @@
username,password
test1,111111
test2,222222
test3,333333
1 username password
2 test1 111111
3 test2 222222
4 test3 333333
@@ -0,0 +1,61 @@
import uuid
from typing import List
import pytest
from httprunner import Config, Step
from loguru import logger
@pytest.fixture(scope="session", autouse=True)
def session_fixture(request):
"""setup and teardown each task"""
total_testcases_num = request.node.testscollected
testcases = []
for item in request.node.items:
testcase = {
"name": item.cls.config.name,
"path": item.cls.config.path,
"node_id": item.nodeid,
}
testcases.append(testcase)
logger.debug(f"collected {total_testcases_num} testcases: {testcases}")
yield
logger.debug("teardown task fixture")
# teardown task
# TODO: upload task summary
@pytest.fixture(scope="function", autouse=True)
def testcase_fixture(request):
"""setup and teardown each testcase"""
config: Config = request.cls.config
teststeps: List[Step] = request.cls.teststeps
logger.debug(f"setup testcase fixture: {config.name} - {request.module.__name__}")
def update_request_headers(steps, index):
for teststep in steps:
if teststep.request:
index += 1
teststep.request.headers["X-Request-ID"] = f"{prefix}-{index}"
elif teststep.testcase and hasattr(teststep.testcase, "teststeps"):
update_request_headers(teststep.testcase.teststeps, index)
# you can update testcase teststep like this
prefix = f"HRUN-{uuid.uuid4()}"
update_request_headers(teststeps, 0)
yield
logger.debug(
f"teardown testcase fixture: {config.name} - {request.module.__name__}"
)
summary = request.instance.get_summary()
logger.debug(f"testcase result summary: {summary}")
# TODO: upload testcase summary
@@ -0,0 +1,55 @@
config:
name: "request methods testcase in hardcode"
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: get with params
request:
method: GET
url: /get
params:
foo1: bar1
foo2: bar2
headers:
:authority: postman-echo.com
:method: POST
:path: /get
:schema: https
User-Agent: HttpRunner/3.0
validate:
- eq: ["status_code", 200]
-
name: post raw text
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."
validate:
- eq: ["status_code", 200]
-
name: post form data
request:
method: POST
url: /post
headers:
User-Agent: HttpRunner/3.0
Content-Type: "application/x-www-form-urlencoded"
data: "foo1=bar1&foo2=bar2"
validate:
- eq: ["status_code", 200]
-
name: put request
request:
method: PUT
url: /put
headers:
User-Agent: HttpRunner/3.0
Content-Type: "text/plain"
data: "This is expected to be sent back as part of response body."
validate:
- eq: ["status_code", 200]
@@ -0,0 +1,68 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/hardcode.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseHardcode(HttpRunner):
config = (
Config("request methods testcase in hardcode")
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get with params")
.get("/get")
.with_params(**{"foo1": "bar1", "foo2": "bar2"})
.with_headers(
**{
":authority": "postman-echo.com",
":method": "POST",
":path": "/get",
":schema": "https",
"User-Agent": "HttpRunner/3.0",
}
)
.validate()
.assert_equal("status_code", 200)
),
Step(
RunRequest("post raw text")
.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.")
.validate()
.assert_equal("status_code", 200)
),
Step(
RunRequest("post form data")
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/3.0",
"Content-Type": "application/x-www-form-urlencoded",
}
)
.with_data("foo1=bar1&foo2=bar2")
.validate()
.assert_equal("status_code", 200)
),
Step(
RunRequest("put request")
.put("/put")
.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.")
.validate()
.assert_equal("status_code", 200)
),
]
if __name__ == "__main__":
TestCaseHardcode().test_start()
@@ -0,0 +1,69 @@
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
weight: 2
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."]
- type_match: ["body.json", None]
- type_match: ["body.json", NoneType]
- type_match: ["body.json", null]
-
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, "response status code should be 200"]
- eq: ["body.form.foo1", "$expect_foo1"]
- eq: ["body.form.foo2", "bar23"]
- eq: ["body.form.foo3", "bar21"]
@@ -0,0 +1,84 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/request_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseRequestWithFunctions(HttpRunner):
config = (
Config("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 = [
Step(
RunRequest("get with params")
.with_variables(
**{"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", "foo3")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.args.foo1", "bar11")
.assert_equal("body.args.sum_v", "3")
.assert_equal("body.args.foo2", "bar21")
),
Step(
RunRequest("post raw text")
.with_variables(**{"foo1": "bar12", "foo3": "bar32"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/${get_httprunner_version()}",
"Content-Type": "text/plain",
}
)
.with_data(
"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: bar12-$expect_foo2-bar32.",
)
.assert_type_match("body.json", "None")
.assert_type_match("body.json", "NoneType")
.assert_type_match("body.json", None)
),
Step(
RunRequest("post form data")
.with_variables(**{"foo2": "bar23"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/${get_httprunner_version()}",
"Content-Type": "application/x-www-form-urlencoded",
}
)
.with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
.validate()
.assert_equal("status_code", 200, "response status code should be 200")
.assert_equal("body.form.foo1", "$expect_foo1")
.assert_equal("body.form.foo2", "bar23")
.assert_equal("body.form.foo3", "bar21")
),
]
if __name__ == "__main__":
TestCaseRequestWithFunctions().test_start()
@@ -0,0 +1,33 @@
config:
name: "request methods testcase: validate with parameters"
parameters:
user_agent: ["iOS/10.1", "iOS/10.2"]
username-password: ${parameterize(request_methods/account.csv)}
app_version: ${get_app_version()}
variables:
app_version: f1
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: get with params
variables:
foo1: $username
foo2: $password
sum_v: "${sum_two(1, $app_version)}"
request:
method: GET
url: /get
params:
foo1: $foo1
foo2: $foo2
sum_v: $sum_v
headers:
User-Agent: $user_agent,$app_version
extract:
session_foo2: "body.args.foo2"
validate:
- eq: ["status_code", 200]
- str_eq: ["body.args.sum_v", "${sum_two(1, $app_version)}"]
# - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] FIXME: TypeError: '<' not supported between instances of 'str' and 'int'
@@ -0,0 +1,53 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/request_with_parameters.yml
import pytest
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import Parameters
class TestCaseRequestWithParameters(HttpRunner):
@pytest.mark.parametrize(
"param",
Parameters(
{
"user_agent": ["iOS/10.1", "iOS/10.2"],
"username-password": "${parameterize(request_methods/account.csv)}",
"app_version": "${get_app_version()}",
}
),
)
def test_start(self, param):
super().test_start(param)
config = (
Config("request methods testcase: validate with parameters")
.variables(**{"app_version": "f1"})
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get with params")
.with_variables(
**{
"foo1": "$username",
"foo2": "$password",
"sum_v": "${sum_two(1, $app_version)}",
}
)
.get("/get")
.with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
.with_headers(**{"User-Agent": "$user_agent,$app_version"})
.extract()
.with_jmespath("body.args.foo2", "session_foo2")
.validate()
.assert_equal("status_code", 200)
.assert_string_equals("body.args.sum_v", "${sum_two(1, $app_version)}")
),
]
if __name__ == "__main__":
TestCaseRequestWithParameters().test_start()
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
@Date : 2022/4/7
@File : request_with_retry.py
@Author : duanchao.bill
@Desc :
"""
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseRetry(HttpRunner):
config = (
Config("request methods testcase in hardcode")
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("run with retry")
.with_retry(retry_times=1, retry_interval=1)
.get("/get")
.with_params(**{"foo1": "${fake_randnum()}"})
.with_headers(**{"User-Agent": "HttpRunner/3.0"})
.validate()
.assert_equal("body.args.foo1", "2")
)
]
@@ -0,0 +1,37 @@
config:
name: "request methods testcase: reference testcase"
variables:
foo1: testsuite_config_bar1
expect_foo1: testsuite_config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: request with functions
variables:
foo1: testcase_ref_bar1
expect_foo1: testcase_ref_bar1
setup_hooks:
- ${sleep(0.1)}
testcase: request_methods/request_with_functions.yml
teardown_hooks:
- ${sleep(0.2)}
export:
- foo3
-
name: post form data
variables:
foo1: bar1
request:
method: POST
url: /post
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
Content-Type: "application/x-www-form-urlencoded"
data: "foo1=$foo1&foo2=$foo3"
validate:
- eq: ["status_code", 200]
- eq: ["body.form.foo1", "bar1"]
- eq: ["body.form.foo2", "bar21"]
@@ -0,0 +1,62 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/request_with_testcase_reference.yml
from httprunner import HttpRunner, Config, Step, RunRequest
from httprunner import RunTestCase
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from request_methods.request_with_functions_test import (
TestCaseRequestWithFunctions as RequestWithFunctions,
)
class TestCaseRequestWithTestcaseReference(HttpRunner):
config = (
Config("request methods testcase: reference testcase")
.variables(
**{
"foo1": "testsuite_config_bar1",
"expect_foo1": "testsuite_config_bar1",
"expect_foo2": "config_bar2",
}
)
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunTestCase("request with functions")
.with_variables(
**{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"}
)
.setup_hook("${sleep(0.1)}")
.call(RequestWithFunctions)
.teardown_hook("${sleep(0.2)}")
.export(*["foo3"])
),
Step(
RunRequest("post form data")
.with_variables(**{"foo1": "bar1"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/${get_httprunner_version()}",
"Content-Type": "application/x-www-form-urlencoded",
}
)
.with_data("foo1=$foo1&foo2=$foo3")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.form.foo1", "bar1")
.assert_equal("body.form.foo2", "bar21")
),
]
if __name__ == "__main__":
TestCaseRequestWithTestcaseReference().test_start()
@@ -0,0 +1,78 @@
config:
name: "request methods testcase with variables"
variables: ${get_testcase_config_variables()}
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: get with params
variables:
foo1: bar11
foo2: bar21
request:
method: GET
url: /get
params:
foo1: $foo1
foo2: $foo2
headers:
User-Agent: HttpRunner/3.0
extract:
foo3: "body.args.foo2"
validate:
- eq: ["status_code", 200]
- eq: ["body.args.foo1", "bar11"]
- eq: ["body.args.foo2", "bar21"]
-
name: post raw text
variables:
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-$foo2-$foo3."
validate:
- eq: ["status_code", 200]
- eq: ["body.data", "This is expected to be sent back as part of response body: bar12-testcase_config_bar2-bar32."]
-
name: post form data
variables:
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&foo3=$foo3"
validate:
- eq: ["status_code", 200]
- eq: ["body.form.foo1", "testcase_config_bar1"]
- eq: ["body.form.foo2", "bar23"]
- 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"]
@@ -0,0 +1,86 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/request_with_variables.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseRequestWithVariables(HttpRunner):
config = (
Config("request methods testcase with variables")
.variables(**{"foo1": "testcase_config_bar1", "foo2": "testcase_config_bar2"})
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get with params")
.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", "foo3")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.args.foo1", "bar11")
.assert_equal("body.args.foo2", "bar21")
),
Step(
RunRequest("post raw text")
.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-$foo2-$foo3."
)
.validate()
.assert_equal("status_code", 200)
.assert_equal(
"body.data",
"This is expected to be sent back as part of response body: bar12-testcase_config_bar2-bar32.",
)
),
Step(
RunRequest("post form data")
.with_variables(**{"foo2": "bar23"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/3.0",
"Content-Type": "application/x-www-form-urlencoded",
}
)
.with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.form.foo1", "testcase_config_bar1")
.assert_equal("body.form.foo2", "bar23")
.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")
),
]
if __name__ == "__main__":
TestCaseRequestWithVariables().test_start()
@@ -0,0 +1,29 @@
config:
name: "request methods testcase: validate with functions"
variables:
foo1: session_bar1
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: get with params
variables:
foo1: bar1
foo2: session_bar2
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:
session_foo2: "body.args.foo2"
validate:
- eq: ["status_code", 200]
- eq: ["body.args.sum_v", "3"]
# - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] FIXME: TypeError: '<' not supported between instances of 'str' and 'int'
@@ -0,0 +1,34 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/validate_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseValidateWithFunctions(HttpRunner):
config = (
Config("request methods testcase: validate with functions")
.variables(**{"foo1": "session_bar1"})
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get with params")
.with_variables(
**{"foo1": "bar1", "foo2": "session_bar2", "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")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.args.sum_v", "3")
),
]
if __name__ == "__main__":
TestCaseValidateWithFunctions().test_start()
@@ -0,0 +1,58 @@
config:
name: "request methods testcase: validate with variables"
variables:
foo1: session_bar1
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: get with params
variables:
foo1: bar1
foo2: session_bar2
request:
method: GET
url: /get
params:
foo1: $foo1
foo2: $foo2
headers:
User-Agent: HttpRunner/3.0
extract:
session_foo2: "body.args.foo2"
validate:
- eq: ["status_code", 200]
- eq: ["body.args.foo1", "$foo1"]
- eq: ["body.args.foo2", "$foo2"]
-
name: post raw text
variables:
foo1: "hello world"
foo3: "$session_foo2"
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."
validate:
- eq: ["status_code", 200]
- eq: ["body.data", "This is expected to be sent back as part of response body: hello world-$foo3."]
-
name: post form data
variables:
foo1: bar1
foo2: bar2
request:
method: POST
url: /post
headers:
User-Agent: HttpRunner/3.0
Content-Type: "application/x-www-form-urlencoded"
data: "foo1=$foo1&foo2=$foo2"
validate:
- eq: ["status_code", 200]
- eq: ["body.form.foo1", "$foo1"]
- eq: ["body.form.foo2", "$foo2"]
@@ -0,0 +1,66 @@
# NOTE: Generated By HttpRunner v4.3.5
# FROM: request_methods/validate_with_variables.yml
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseValidateWithVariables(HttpRunner):
config = (
Config("request methods testcase: validate with variables")
.variables(**{"foo1": "session_bar1"})
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get with params")
.with_variables(**{"foo1": "bar1", "foo2": "session_bar2"})
.get("/get")
.with_params(**{"foo1": "$foo1", "foo2": "$foo2"})
.with_headers(**{"User-Agent": "HttpRunner/3.0"})
.extract()
.with_jmespath("body.args.foo2", "session_foo2")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.args.foo1", "$foo1")
.assert_equal("body.args.foo2", "$foo2")
),
Step(
RunRequest("post raw text")
.with_variables(**{"foo1": "hello world", "foo3": "$session_foo2"})
.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."
)
.validate()
.assert_equal("status_code", 200)
.assert_equal(
"body.data",
"This is expected to be sent back as part of response body: hello world-$foo3.",
)
),
Step(
RunRequest("post form data")
.with_variables(**{"foo1": "bar1", "foo2": "bar2"})
.post("/post")
.with_headers(
**{
"User-Agent": "HttpRunner/3.0",
"Content-Type": "application/x-www-form-urlencoded",
}
)
.with_data("foo1=$foo1&foo2=$foo2")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.form.foo1", "$foo1")
.assert_equal("body.form.foo2", "$foo2")
),
]
if __name__ == "__main__":
TestCaseValidateWithVariables().test_start()