mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-15 04:19:28 +08:00
refactor: parameters
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from httprunner import loader
|
||||
from httprunner.make import (
|
||||
main_make,
|
||||
convert_testcase_path,
|
||||
@@ -9,9 +10,7 @@ from httprunner.make import (
|
||||
make_teststep_chain_style,
|
||||
pytest_files_run_set,
|
||||
ensure_file_abs_path_valid,
|
||||
make_test_start_style,
|
||||
)
|
||||
from httprunner import loader
|
||||
|
||||
|
||||
class TestMake(unittest.TestCase):
|
||||
@@ -215,24 +214,3 @@ from request_methods.request_with_functions_test import (
|
||||
teststep_chain_style,
|
||||
"""Step(RunRequest("get with params").with_variables(**{'foo1': 'bar1', 'foo2': 123, '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.foo1', 'session_foo1').with_jmespath('body.args.foo2', 'session_foo2').validate().assert_equal("status_code", 200).assert_equal("body.args.sum_v", "3"))""",
|
||||
)
|
||||
|
||||
def test_make_test_start_style(self):
|
||||
params = {
|
||||
"user_agent": ["iOS/10.1", "iOS/10.2"],
|
||||
"username-password": "${parameterize(request_methods/account.csv)}",
|
||||
"app_version": "${get_app_version()}",
|
||||
}
|
||||
config = {"parameters": params}
|
||||
self.assertEqual(
|
||||
make_test_start_style(config),
|
||||
f"""
|
||||
import pytest
|
||||
from httprunner.parser import parse_parameters
|
||||
|
||||
param = [{params}]
|
||||
|
||||
@pytest.mark.parametrize('parametrize', parse_parameters(param))
|
||||
def test_start(self, parametrize):
|
||||
super().test_start(parametrize)
|
||||
""",
|
||||
)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from httprunner import parser
|
||||
from httprunner.exceptions import VariableNotFound, FunctionNotFound
|
||||
from httprunner.parser import regex_findall_variables
|
||||
from httprunner.loader import load_project_meta
|
||||
|
||||
|
||||
class TestParserBasic(unittest.TestCase):
|
||||
@@ -27,17 +28,19 @@ class TestParserBasic(unittest.TestCase):
|
||||
self.assertEqual(parser.parse_string_value("${func}"), "${func}")
|
||||
|
||||
def test_regex_findall_variables(self):
|
||||
self.assertEqual(regex_findall_variables("$variable"), ["variable"])
|
||||
self.assertEqual(regex_findall_variables("${variable}123"), ["variable"])
|
||||
self.assertEqual(regex_findall_variables("/blog/$postid"), ["postid"])
|
||||
self.assertEqual(regex_findall_variables("/$var1/$var2"), ["var1", "var2"])
|
||||
self.assertEqual(regex_findall_variables("abc"), [])
|
||||
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$a"), ["a"])
|
||||
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$a"), [])
|
||||
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$$a"), ["a"])
|
||||
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$$$a"), [])
|
||||
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$a$b"), ["b"])
|
||||
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$a$$b"), [])
|
||||
self.assertEqual(parser.regex_findall_variables("$variable"), ["variable"])
|
||||
self.assertEqual(parser.regex_findall_variables("${variable}123"), ["variable"])
|
||||
self.assertEqual(parser.regex_findall_variables("/blog/$postid"), ["postid"])
|
||||
self.assertEqual(
|
||||
parser.regex_findall_variables("/$var1/$var2"), ["var1", "var2"]
|
||||
)
|
||||
self.assertEqual(parser.regex_findall_variables("abc"), [])
|
||||
self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$a"), ["a"])
|
||||
self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$a"), [])
|
||||
self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$$a"), ["a"])
|
||||
self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$$$a"), [])
|
||||
self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$a$b"), ["b"])
|
||||
self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$a$$b"), [])
|
||||
|
||||
def test_extract_variables(self):
|
||||
self.assertEqual(parser.extract_variables("$var"), {"var"})
|
||||
@@ -454,23 +457,22 @@ class TestParserBasic(unittest.TestCase):
|
||||
self.assertEqual(parsed_testcase["headers"]["sum"], 3)
|
||||
|
||||
def test_parse_parameters_testcase(self):
|
||||
variables = {
|
||||
"user_agent": "chrome",
|
||||
"sum": 5,
|
||||
parameters = {
|
||||
"user_agent": ["iOS/10.1", "iOS/10.2"],
|
||||
"username-password": "${parameterize(request_methods/account.csv)}",
|
||||
"sum": "${calculate_two_nums(1, 2)}",
|
||||
}
|
||||
param = [
|
||||
{
|
||||
"user_agent": ["iOS/10.1", "iOS/10.2"],
|
||||
"username-password": "${parameterize(request_methods/account.csv)}",
|
||||
"sum": "${add_two_nums(1, 2)}",
|
||||
}
|
||||
]
|
||||
load_project_meta(
|
||||
os.path.join(
|
||||
os.path.dirname(os.path.dirname(__file__)),
|
||||
"examples",
|
||||
"postman_echo",
|
||||
"request_methods",
|
||||
),
|
||||
)
|
||||
parsed_params = parser.parse_parameters(parameters)
|
||||
self.assertEqual(len(parsed_params), 2 * 3 * 2)
|
||||
|
||||
functions = {
|
||||
"add_two_nums": lambda a, b=1: [a + b, b - a],
|
||||
}
|
||||
|
||||
parsed_params = parser.parse_parameters(param, variables, functions)
|
||||
self.assertIn(
|
||||
{
|
||||
"username": "test1",
|
||||
|
||||
@@ -126,12 +126,6 @@ class TestUtils(unittest.TestCase):
|
||||
{"base_url": "https://httpbin.org", "foo1": "bar1"},
|
||||
)
|
||||
|
||||
def test_ensure_mapping_format(self):
|
||||
map_list = [{"a": 1}, {"b": 2}]
|
||||
ordered_dict = utils.ensure_mapping_format(map_list)
|
||||
self.assertIsInstance(ordered_dict, dict)
|
||||
self.assertIn("a", ordered_dict)
|
||||
|
||||
def test_cartesian_product_one(self):
|
||||
parameters_content_list = [[{"a": 1}, {"a": 2}]]
|
||||
product_list = utils.gen_cartesian_product(*parameters_content_list)
|
||||
|
||||
Reference in New Issue
Block a user