From c504f305f10dc66a6d04f1a55e88ec4ad1cc67c7 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 8 Feb 2018 15:59:17 +0800 Subject: [PATCH] #96: refactor skip feature: 1, with skip keyword, skip current test unconditionally; 2, with skipIf keyword, you can skip current test with condition; condition evaluation can be defined in debugtalk.py function. --- httprunner/context.py | 2 +- httprunner/runner.py | 7 +++++++ httprunner/task.py | 6 +++--- tests/data/debugtalk.py | 8 ++++++++ tests/data/demo_testset_cli.yml | 25 +++++++++++++++++++++++-- tests/test_cli.py | 4 ++-- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/httprunner/context.py b/httprunner/context.py index 967f36a2..5e134605 100644 --- a/httprunner/context.py +++ b/httprunner/context.py @@ -167,7 +167,7 @@ class Context(object): def exec_content_functions(self, content): """ execute functions in content. """ - self.testcase_parser.eval_content_functions(content) + return self.testcase_parser.eval_content_functions(content) def eval_check_item(self, validator, resp_obj): """ evaluate check item in validator diff --git a/httprunner/runner.py b/httprunner/runner.py index 46d7f63a..17c5c04e 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -1,4 +1,5 @@ import logging +from unittest.case import SkipTest from httprunner import exception, response, testcase, utils from httprunner.client import HttpSession @@ -107,6 +108,12 @@ class Runner(object): setup_actions = testcase_dict.get("setup", []) teardown_actions = testcase_dict.get("teardown", []) + if "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) + def setup_teardown(actions): for action in actions: self.context.exec_content_functions(action) diff --git a/httprunner/task.py b/httprunner/task.py index 4134dfe1..03dbd879 100644 --- a/httprunner/task.py +++ b/httprunner/task.py @@ -15,9 +15,9 @@ class ApiTestCase(unittest.TestCase): def runTest(self): """ run testcase and check result. """ - skip_current_test = self.testcase_dict.get("skip", False) - if skip_current_test: - self.skipTest("skip this test") + if "skip" in self.testcase_dict: + skip_reason = self.testcase_dict["skip"] + self.skipTest(skip_reason) else: self.assertTrue(self.test_runner._run_test(self.testcase_dict)) diff --git a/tests/data/debugtalk.py b/tests/data/debugtalk.py index 1c5d1889..808e0d2f 100644 --- a/tests/data/debugtalk.py +++ b/tests/data/debugtalk.py @@ -1,6 +1,7 @@ import hashlib import hmac import json +import os import random import string import time @@ -40,3 +41,10 @@ def sum_status_code(status_code, expect_sum): sum_value += int(digit) assert sum_value == expect_sum + +os.environ["TEST_ENV"] = "PRODUCTION" + +def skip_test_in_production_env(): + """ skip this test in production environment + """ + return os.environ["TEST_ENV"] == "PRODUCTION" diff --git a/tests/data/demo_testset_cli.yml b/tests/data/demo_testset_cli.yml index 191ee84b..743cdf14 100644 --- a/tests/data/demo_testset_cli.yml +++ b/tests/data/demo_testset_cli.yml @@ -68,8 +68,29 @@ - {"check": "content.success", "comparator": "eq", "expect": false} - test: - name: create user which existed (skipped) - skip: True + name: create user which existed (skip unconditionally) + skip: skip this test unconditionally + 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} + +- test: + name: create user which existed (skip with condition) + skipIf: ${skip_test_in_production_env()} times: 2 request: url: http://127.0.0.1:5000/api/users/1000 diff --git a/tests/test_cli.py b/tests/test_cli.py index db18a7f3..eec6f6eb 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -26,10 +26,10 @@ class TestCli(ApiServerUnittest): def test_run_times(self): result = HTMLTestRunner(**self.kwargs).run(self.task_suite) - self.assertEqual(result.testsRun, 6) + self.assertEqual(result.testsRun, 8) shutil.rmtree(self.report_save_dir) def test_skip(self): result = HTMLTestRunner(**self.kwargs).run(self.task_suite) - self.assertEqual(len(result.skipped), 2) + self.assertEqual(len(result.skipped), 4) shutil.rmtree(self.report_save_dir)