test: add unittest for ensure_testcase_v3

This commit is contained in:
debugtalk
2020-05-28 11:14:24 +08:00
parent 06b219fd06
commit 803881f5b9
2 changed files with 115 additions and 22 deletions

View File

@@ -2,7 +2,7 @@
This module handles compatibility issues between testcase format v2 and v3.
"""
from typing import List, Dict, Text
from typing import List, Dict, Text, Union
from httprunner import exceptions
@@ -29,7 +29,7 @@ def convert_jmespath(raw: Text) -> Text:
return ".".join(raw_list).replace(".[", "[")
def convert_extractors(extractors: List) -> Dict:
def convert_extractors(extractors: Union[List, Dict]) -> Dict:
""" convert extract list(v2) to dict(v3)
Args:
@@ -71,19 +71,34 @@ def convert_validators(validators: List) -> List:
return validators
def ensure_step_attachment(step: Dict) -> Dict:
test_dict = {
"name": step["name"],
}
if "variables" in step:
test_dict["variables"] = step["variables"]
if "extract" in step:
test_dict["extract"] = convert_extractors(step["extract"])
if "validate" in step:
test_dict["validate"] = convert_validators(step["validate"])
return test_dict
def ensure_testcase_v3_api(api_content: Dict) -> Dict:
teststep = {
"request": api_content["request"],
}
teststep.update(ensure_step_attachment(api_content))
return {
"config": {"name": api_content["name"]},
"teststeps": [
{
"name": api_content["name"],
"variables": api_content.get("variables", {}),
"request": api_content["request"],
"validate": convert_validators(api_content.get("validate", [])),
"extract": convert_extractors(api_content.get("extract", {})),
}
],
"teststeps": [teststep],
}
@@ -92,9 +107,7 @@ def ensure_testcase_v3(test_content: Dict) -> Dict:
v3_content = {"config": test_content["config"], "teststeps": []}
for step in test_content["teststeps"]:
teststep = {"name": step.pop("name", "")}
if "variables" in step:
teststep["variables"] = step.pop("variables")
teststep = {}
if "request" in step:
teststep["request"] = step.pop("request")
@@ -103,13 +116,7 @@ def ensure_testcase_v3(test_content: Dict) -> Dict:
elif "testcase" in step:
teststep["testcase"] = step.pop("testcase")
if "extract" in step:
teststep["extract"] = convert_extractors(step.pop("extract"))
if "validate" in step:
teststep["validate"] = convert_validators(step.pop("validate"))
teststep.update(step)
teststep.update(ensure_step_attachment(step))
v3_content["teststeps"].append(teststep)
return v3_content

View File

@@ -1,4 +1,3 @@
import os
import unittest
from httprunner import compat
@@ -48,3 +47,90 @@ class TestCompat(unittest.TestCase):
compat.convert_validators([{"eq": ["content.0.name", 201]}]),
[{"eq": ["body[0].name", 201]}],
)
def test_ensure_testcase_v3_api(self):
api_content = {
"name": "get with params",
"request": {
"method": "GET",
"url": "/get",
"params": {"foo1": "bar1", "foo2": "bar2"},
"headers": {"User-Agent": "HttpRunner/3.0"},
},
"extract": [{"varA": "content.varA"}, {"user_agent": "headers.User-Agent"}],
"validate": [{"eq": ["content.varB", 200]}, {"lt": ["json.0.varC", 0]}],
}
self.assertEqual(
compat.ensure_testcase_v3_api(api_content),
{
"config": {"name": "get with params"},
"teststeps": [
{
"name": "get with params",
"request": {
"method": "GET",
"url": "/get",
"params": {"foo1": "bar1", "foo2": "bar2"},
"headers": {"User-Agent": "HttpRunner/3.0"},
},
"extract": {
"varA": "body.varA",
"user_agent": 'headers."User-Agent"',
},
"validate": [
{"eq": ["body.varB", 200]},
{"lt": ["body[0].varC", 0]},
],
}
],
},
)
def test_ensure_testcase_v3(self):
testcase_content = {
"config": {"name": "xxx", "base_url": "https://httpbin.org"},
"teststeps": [
{
"name": "get with params",
"request": {
"method": "GET",
"url": "/get",
"params": {"foo1": "bar1", "foo2": "bar2"},
"headers": {"User-Agent": "HttpRunner/3.0"},
},
"extract": [
{"varA": "content.varA"},
{"user_agent": "headers.User-Agent"},
],
"validate": [
{"eq": ["content.varB", 200]},
{"lt": ["json.0.varC", 0]},
],
}
],
}
self.assertEqual(
compat.ensure_testcase_v3(testcase_content),
{
"config": {"name": "xxx", "base_url": "https://httpbin.org"},
"teststeps": [
{
"name": "get with params",
"request": {
"method": "GET",
"url": "/get",
"params": {"foo1": "bar1", "foo2": "bar2"},
"headers": {"User-Agent": "HttpRunner/3.0"},
},
"extract": {
"varA": "body.varA",
"user_agent": 'headers."User-Agent"',
},
"validate": [
{"eq": ["body.varB", 200]},
{"lt": ["body[0].varC", 0]},
],
}
],
},
)