From b3eede8762f91e867443219a10766acf9ef0dbd5 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 22 Aug 2018 23:20:15 +0800 Subject: [PATCH] fix #309 #312 --- httprunner/utils.py | 2 ++ tests/data/demo_testcase.yml | 23 +++++++++++++++++++++++ tests/debugtalk.py | 9 +++++++++ tests/test_api.py | 15 +++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 tests/data/demo_testcase.yml diff --git a/httprunner/utils.py b/httprunner/utils.py index 395a70c7..c4a76220 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -333,6 +333,8 @@ def print_io(in_out): for variable, value in in_out.items(): if isinstance(value, tuple): continue + elif isinstance(value, dict): + value = json.dumps(value) if is_py2: if isinstance(variable, unicode): diff --git a/tests/data/demo_testcase.yml b/tests/data/demo_testcase.yml new file mode 100644 index 00000000..e16eea4d --- /dev/null +++ b/tests/data/demo_testcase.yml @@ -0,0 +1,23 @@ +- config: + name: "123$var_a" + variables: + - var_a: 0 + - var_c: "${sum_two(1, 2)}" + parameters: + - "var_a-var_b": + - [11, 21] + - [12, 22] + - "app_version": "${gen_app_version()}" + request: $demo_default_request + +- test: + name: testcase1-$var_a + request: + url: /api1 + method: GET + headers: + var_a: $var_a + var_b: $var_b + var_c: $var_c + validate: + - {"check": "status_code", "comparator": "eq", "expect": 200} diff --git a/tests/debugtalk.py b/tests/debugtalk.py index 83f91ad3..d232fe0e 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -8,6 +8,15 @@ from tests.api_server import HTTPBIN_SERVER, SECRET_KEY, gen_md5, get_sign BASE_URL = "http://127.0.0.1:5000" +demo_default_request = { + "base_url": "$BASE_URL", + "headers": { + "content-type": "application/json" + } +} + +def sum_two(m, n): + return m + n def sum_status_code(status_code, expect_sum): """ sum status code digits diff --git a/tests/test_api.py b/tests/test_api.py index bdc3c7fa..9baf5338 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -359,6 +359,21 @@ class TestHttpRunner(ApiServerUnittest): runner.project_mapping["debugtalk"]["functions"] ) + def test_parse_tests(self): + testcase_file_path = os.path.join( + os.getcwd(), 'tests/data/demo_testcase.yml') + runner = HttpRunner() + testcases = runner.load_tests(testcase_file_path) + parsed_testcases = runner.parse_tests(testcases) + self.assertEqual(parsed_testcases[0]["config"]["variables"]["var_c"], 3) + self.assertEqual(len(parsed_testcases), 2 * 2) + self.assertEqual( + parsed_testcases[0]["config"]["request"]["base_url"], + 'http://127.0.0.1:5000' + ) + self.assertIsInstance(parsed_testcases, list) + self.assertEqual(parsed_testcases[0]["config"]["name"], '12311') + class TestLocustRunner(ApiServerUnittest):