Skip to content

Commit

Permalink
Merge pull request httprunner#975 from httprunner/dev
Browse files Browse the repository at this point in the history
## 3.1.4 (2020-07-30)

**Changed**

- change: override variables strategy, step variables > extracted variables from previous steps

**Fixed**

- fix: parameters feature with custom functions
- fix: request json field with variable reference
- fix: pickle BufferedReader TypeError in upload feature
  • Loading branch information
debugtalk authored Jul 30, 2020
2 parents 0c595ba + 0dbd8c5 commit 4796e0a
Show file tree
Hide file tree
Showing 26 changed files with 124 additions and 47 deletions.
12 changes: 12 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Release History

## 3.1.4 (2020-07-30)

**Changed**

- change: override variables strategy, step variables > extracted variables from previous steps

**Fixed**

- fix: parameters feature with custom functions
- fix: request json field with variable reference
- fix: pickle BufferedReader TypeError in upload feature

## 3.1.3 (2020-07-06)

**Added**
Expand Down
2 changes: 1 addition & 1 deletion examples/httpbin/basic_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: basic.yml


Expand Down
2 changes: 1 addition & 1 deletion examples/httpbin/hooks_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: hooks.yml


Expand Down
2 changes: 1 addition & 1 deletion examples/httpbin/load_image_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: load_image.yml


Expand Down
2 changes: 1 addition & 1 deletion examples/httpbin/upload_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: upload.yml


Expand Down
2 changes: 1 addition & 1 deletion examples/httpbin/validate_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: validate.yml


Expand Down
64 changes: 64 additions & 0 deletions examples/postman_echo/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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(f"start running testcases ...")

start_at = time.time()

yield

logger.info(f"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_datas)
if testcase_summary.success:
summary["stat"]["testcases"]["success"] += 1
summary["stat"]["teststeps"]["successes"] += len(
testcase_summary.step_datas
)
else:
summary["stat"]["testcases"]["fail"] += 1
summary["stat"]["teststeps"]["successes"] += (
len(testcase_summary.step_datas) - 1
)
summary["stat"]["teststeps"]["failures"] += 1

testcase_summary_json = testcase_summary.dict()
testcase_summary_json["records"] = testcase_summary_json.pop("step_datas")
summary["details"].append(testcase_summary_json)

summary_path = "/Users/debugtalk/MyProjects/HttpRunner-dev/HttpRunner/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}")

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/request_with_functions.yml


Expand Down Expand Up @@ -57,7 +57,7 @@ class TestCaseRequestWithFunctions(HttpRunner):
.assert_equal("status_code", 200)
.assert_equal(
"body.data",
"This is expected to be sent back as part of response body: bar12-$expect_foo2-bar21.",
"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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/request_with_testcase_reference.yml


Expand Down
2 changes: 1 addition & 1 deletion examples/postman_echo/request_methods/hardcode_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/hardcode.yml


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ teststeps:
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-bar21."]
- 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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/request_with_functions.yml


Expand Down Expand Up @@ -57,7 +57,7 @@ class TestCaseRequestWithFunctions(HttpRunner):
.assert_equal("status_code", 200)
.assert_equal(
"body.data",
"This is expected to be sent back as part of response body: bar12-$expect_foo2-bar21.",
"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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/request_with_parameters.yml


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/request_with_testcase_reference.yml


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ teststeps:
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-bar21."]
- 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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NOTE: Generated By HttpRunner v3.1.3
# FROM: request_methods\request_with_variables.yml
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/request_with_variables.yml


from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
Expand Down Expand Up @@ -42,7 +42,7 @@ class TestCaseRequestWithVariables(HttpRunner):
.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-bar21.",
"This is expected to be sent back as part of response body: bar12-testcase_config_bar2-bar32.",
)
),
Step(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/validate_with_functions.yml


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.1.3
# NOTE: Generated By HttpRunner v3.1.4
# FROM: request_methods/validate_with_variables.yml


Expand Down
2 changes: 1 addition & 1 deletion httprunner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "3.1.3"
__version__ = "3.1.4"
__description__ = "One-stop solution for HTTP(S) testing."

# import firstly for monkey patch if needed
Expand Down
6 changes: 2 additions & 4 deletions httprunner/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
convert_relative_project_root_dir,
)
from httprunner.response import uniform_validator
from httprunner.utils import override_config_variables, is_support_multiprocessing
from httprunner.utils import merge_variables, is_support_multiprocessing

""" cache converted pytest files, avoid duplicate making
"""
Expand Down Expand Up @@ -485,9 +485,7 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
testcase_variables = convert_variables(
testcase.get("variables", {}), testcase_path
)
testcase_variables = override_config_variables(
testcase_variables, testsuite_variables
)
testcase_variables = merge_variables(testcase_variables, testsuite_variables)
# testsuite testcase variables > testcase config variables
testcase_dict["config"]["variables"] = convert_variables(
testcase_dict["config"].get("variables", {}), testcase_path
Expand Down
16 changes: 8 additions & 8 deletions httprunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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.utils import merge_variables
from httprunner.models import (
TConfig,
TStep,
Expand Down Expand Up @@ -335,17 +335,16 @@ def run_testcase(self, testcase: TestCase) -> "HttpRunner":
self.__start_at = time.time()
self.__step_datas: List[StepData] = []
self.__session = self.__session or HttpSession()
self.__session_variables = {}
# save extracted variables of teststeps
extracted_variables: VariablesMapping = {}

# run teststeps
for step in self.__teststeps:
# override variables
# session variables (extracted from pre step) > step variables
step.variables.update(self.__session_variables)
# step variables > extracted variables from previous steps
step.variables = merge_variables(step.variables, extracted_variables)
# step variables > testcase config variables
step.variables = override_config_variables(
step.variables, self.__config.variables
)
step.variables = merge_variables(step.variables, self.__config.variables)

# parse variables
step.variables = parse_variables_mapping(
Expand All @@ -360,8 +359,9 @@ def run_testcase(self, testcase: TestCase) -> "HttpRunner":
extract_mapping = self.__run_step(step)

# save extracted variables to session variables
self.__session_variables.update(extract_mapping)
extracted_variables.update(extract_mapping)

self.__session_variables.update(extracted_variables)
self.__duration = time.time() - self.__start_at
return self

Expand Down
2 changes: 1 addition & 1 deletion httprunner/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def create_file(path, file_content=""):
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-bar21."]
- eq: ["body.data", "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32."]
-
name: post form data
variables:
Expand Down
16 changes: 7 additions & 9 deletions httprunner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,23 @@ def default(self, obj):
return repr(obj)


def override_config_variables(
step_variables: VariablesMapping, config_variables: VariablesMapping
def merge_variables(
variables: VariablesMapping, variables_to_be_overridden: VariablesMapping
) -> VariablesMapping:
""" override variables:
testcase step variables > testcase config variables
testsuite testcase variables > testsuite config variables
""" merge two variables mapping, the first variables have higher priority
"""
step_new_variables = {}
for key, value in step_variables.items():
for key, value in variables.items():
if f"${key}" == value or "${" + key + "}" == value:
# e.g. {"base_url": "$base_url"}
# or {"base_url": "${base_url}"}
continue

step_new_variables[key] = value

variables = copy.deepcopy(config_variables)
variables.update(step_new_variables)
return variables
merged_variables = copy.copy(variables_to_be_overridden)
merged_variables.update(step_new_variables)
return merged_variables


def is_support_multiprocessing() -> bool:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "httprunner"
version = "3.1.3"
version = "3.1.4"
description = "One-stop solution for HTTP(S) testing."
license = "Apache-2.0"
readme = "README.md"
Expand Down
7 changes: 6 additions & 1 deletion tests/make_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ def test_make_teststep_chain_style(self):
def test_make_requests_with_json_chain_style(self):
step = {
"name": "get with params",
"variables": {"foo1": "bar1", "foo2": 123, "sum_v": "${sum_two(1, 2)}","myjson":{"name": "user", "password": "123456"}},
"variables": {
"foo1": "bar1",
"foo2": 123,
"sum_v": "${sum_two(1, 2)}",
"myjson": {"name": "user", "password": "123456"},
},
"request": {
"method": "GET",
"url": "/get",
Expand Down
4 changes: 2 additions & 2 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from httprunner import loader, utils
from httprunner.utils import (
ExtendJSONEncoder,
override_config_variables,
merge_variables,
)


Expand Down Expand Up @@ -122,7 +122,7 @@ 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),
merge_variables(step_variables, config_variables),
{"base_url": "https://httpbin.org", "foo1": "bar1"},
)

Expand Down

0 comments on commit 4796e0a

Please sign in to comment.