mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 22:44:05 +08:00
#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:
@@ -167,7 +167,7 @@ class Context(object):
|
|||||||
def exec_content_functions(self, content):
|
def exec_content_functions(self, content):
|
||||||
""" execute functions in 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):
|
def eval_check_item(self, validator, resp_obj):
|
||||||
""" evaluate check item in validator
|
""" evaluate check item in validator
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from unittest.case import SkipTest
|
||||||
|
|
||||||
from httprunner import exception, response, testcase, utils
|
from httprunner import exception, response, testcase, utils
|
||||||
from httprunner.client import HttpSession
|
from httprunner.client import HttpSession
|
||||||
@@ -107,6 +108,12 @@ class Runner(object):
|
|||||||
setup_actions = testcase_dict.get("setup", [])
|
setup_actions = testcase_dict.get("setup", [])
|
||||||
teardown_actions = testcase_dict.get("teardown", [])
|
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):
|
def setup_teardown(actions):
|
||||||
for action in actions:
|
for action in actions:
|
||||||
self.context.exec_content_functions(action)
|
self.context.exec_content_functions(action)
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ class ApiTestCase(unittest.TestCase):
|
|||||||
def runTest(self):
|
def runTest(self):
|
||||||
""" run testcase and check result.
|
""" run testcase and check result.
|
||||||
"""
|
"""
|
||||||
skip_current_test = self.testcase_dict.get("skip", False)
|
if "skip" in self.testcase_dict:
|
||||||
if skip_current_test:
|
skip_reason = self.testcase_dict["skip"]
|
||||||
self.skipTest("skip this test")
|
self.skipTest(skip_reason)
|
||||||
else:
|
else:
|
||||||
self.assertTrue(self.test_runner._run_test(self.testcase_dict))
|
self.assertTrue(self.test_runner._run_test(self.testcase_dict))
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import time
|
import time
|
||||||
@@ -40,3 +41,10 @@ def sum_status_code(status_code, expect_sum):
|
|||||||
sum_value += int(digit)
|
sum_value += int(digit)
|
||||||
|
|
||||||
assert sum_value == expect_sum
|
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"
|
||||||
|
|||||||
@@ -68,8 +68,29 @@
|
|||||||
- {"check": "content.success", "comparator": "eq", "expect": false}
|
- {"check": "content.success", "comparator": "eq", "expect": false}
|
||||||
|
|
||||||
- test:
|
- test:
|
||||||
name: create user which existed (skipped)
|
name: create user which existed (skip unconditionally)
|
||||||
skip: True
|
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
|
times: 2
|
||||||
request:
|
request:
|
||||||
url: http://127.0.0.1:5000/api/users/1000
|
url: http://127.0.0.1:5000/api/users/1000
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ class TestCli(ApiServerUnittest):
|
|||||||
|
|
||||||
def test_run_times(self):
|
def test_run_times(self):
|
||||||
result = HTMLTestRunner(**self.kwargs).run(self.task_suite)
|
result = HTMLTestRunner(**self.kwargs).run(self.task_suite)
|
||||||
self.assertEqual(result.testsRun, 6)
|
self.assertEqual(result.testsRun, 8)
|
||||||
shutil.rmtree(self.report_save_dir)
|
shutil.rmtree(self.report_save_dir)
|
||||||
|
|
||||||
def test_skip(self):
|
def test_skip(self):
|
||||||
result = HTMLTestRunner(**self.kwargs).run(self.task_suite)
|
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)
|
shutil.rmtree(self.report_save_dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user