#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.
This commit is contained in:
httprunner
2018-02-08 15:59:17 +08:00
parent 5bbbdc1997
commit 9042e6e72f
6 changed files with 44 additions and 8 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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))

View File

@@ -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"

View File

@@ -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

View File

@@ -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)