From 3936b82a52a425b06ebb17a147c86a84c17baea3 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 8 Feb 2018 22:36:30 +0800 Subject: [PATCH] #96: skip unless condition --- httprunner/runner.py | 33 +++++++++++++++++++++++++-------- tests/data/demo_testset_cli.yml | 23 ++++++++++++++++++++++- tests/test_cli.py | 2 +- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/httprunner/runner.py b/httprunner/runner.py index 628d5082..128e0afc 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -67,6 +67,30 @@ class Runner(object): return parsed_request + def _handle_skip_feature(self, testcase_dict): + """ handle skip feature for testcase + - skip: skip current test unconditionally + - skipIf: skip current test if condition is true + - skipUnless: skip current test unless condition is true + """ + skip_reason = None + + if "skip" in testcase_dict: + skip_reason = testcase_dict["skip"] + + elif "skipIf" in testcase_dict: + skip_if_condition = testcase_dict["skipIf"] + if self.context.exec_content_functions(skip_if_condition): + skip_reason = "{} evaluate to True".format(skip_if_condition) + + elif "skipUnless" in testcase_dict: + skip_unless_condition = testcase_dict["skipUnless"] + if not self.context.exec_content_functions(skip_unless_condition): + skip_reason = "{} evaluate to False".format(skip_unless_condition) + + if skip_reason: + raise SkipTest(skip_reason) + def _run_test(self, testcase_dict): """ run single testcase. @param (dict) testcase_dict @@ -108,14 +132,7 @@ class Runner(object): setup_actions = testcase_dict.get("setup", []) teardown_actions = testcase_dict.get("teardown", []) - if "skip" in testcase_dict: - skip_reason = testcase_dict["skip"] - raise SkipTest(skip_reason) - elif "skipIf" in testcase_dict: - skip_if_condition = testcase_dict["skipIf"] - if self.context.exec_content_functions(skip_if_condition): - skip_reason = "{} evaluate to True".format(skip_if_condition) - raise SkipTest(skip_reason) + self._handle_skip_feature(testcase_dict) def setup_teardown(actions): for action in actions: diff --git a/tests/data/demo_testset_cli.yml b/tests/data/demo_testset_cli.yml index 743cdf14..4d848c9e 100644 --- a/tests/data/demo_testset_cli.yml +++ b/tests/data/demo_testset_cli.yml @@ -89,7 +89,7 @@ - {"check": "content.success", "comparator": "eq", "expect": false} - test: - name: create user which existed (skip with condition) + name: create user which existed (skip if condition) skipIf: ${skip_test_in_production_env()} times: 2 request: @@ -108,3 +108,24 @@ - "eq": ["content.success", false] - {"check": "status_code", "comparator": "eq", "expect": 500} - {"check": "content.success", "comparator": "eq", "expect": false} + +- test: + name: create user which existed (skip unless condition) + skipUnless: ${skip_test_in_production_env()} + times: 2 + request: + url: http://127.0.0.1:5000/api/users/1000 + method: POST + headers: + Content-Type: application/json + device_sn: 'HZfFBh6tU59EdXJ' + token: $token + json: + name: "user1" + password: "123456" + validate: + - "eq": ["status_code", 500] + - sum_status_code: ["status_code", 5] + - "eq": ["content.success", false] + - {"check": "status_code", "comparator": "eq", "expect": 500} + - {"check": "content.success", "comparator": "eq", "expect": false} diff --git a/tests/test_cli.py b/tests/test_cli.py index eec6f6eb..a9a7b902 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -26,7 +26,7 @@ class TestCli(ApiServerUnittest): def test_run_times(self): result = HTMLTestRunner(**self.kwargs).run(self.task_suite) - self.assertEqual(result.testsRun, 8) + self.assertEqual(result.testsRun, 10) shutil.rmtree(self.report_save_dir) def test_skip(self):