mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
TestRunner: run testcase writen in separate template
This commit is contained in:
@@ -1,12 +1,39 @@
|
|||||||
import requests
|
import requests
|
||||||
from ate import utils, exception
|
|
||||||
|
from ate import exception, utils
|
||||||
|
from ate.context import Context
|
||||||
|
from ate.testcase import TestcaseParser
|
||||||
|
|
||||||
|
|
||||||
class TestRunner(object):
|
class TestRunner(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.client = requests.Session()
|
self.client = requests.Session()
|
||||||
|
self.context = Context()
|
||||||
|
self.testcase_parser = TestcaseParser()
|
||||||
|
|
||||||
|
def prepare(self, testcase):
|
||||||
|
""" prepare work before running test.
|
||||||
|
parse testcase with variables binds if it is a template.
|
||||||
|
"""
|
||||||
|
requires = testcase.get('requires', [])
|
||||||
|
self.context.import_requires(requires)
|
||||||
|
|
||||||
|
function_binds = testcase.get('function_binds', {})
|
||||||
|
self.context.bind_functions(function_binds)
|
||||||
|
|
||||||
|
variable_binds = testcase.get('variable_binds', [])
|
||||||
|
self.context.bind_variables(variable_binds)
|
||||||
|
|
||||||
|
parsed_testcase = self.testcase_parser.parse(
|
||||||
|
testcase,
|
||||||
|
variables_binds=self.context.variables
|
||||||
|
)
|
||||||
|
return parsed_testcase
|
||||||
|
|
||||||
def run_single_testcase(self, testcase):
|
def run_single_testcase(self, testcase):
|
||||||
|
testcase = self.prepare(testcase)
|
||||||
|
|
||||||
req_kwargs = testcase['request']
|
req_kwargs = testcase['request']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
62
test/data/demo_template_separate.yml
Normal file
62
test/data/demo_template_separate.yml
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
-
|
||||||
|
name: create user which does not exist
|
||||||
|
requires:
|
||||||
|
- random
|
||||||
|
- string
|
||||||
|
- hashlib
|
||||||
|
function_binds:
|
||||||
|
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
|
||||||
|
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||||
|
variable_binds:
|
||||||
|
- TOKEN: debugtalk
|
||||||
|
- random: {"func": "gen_random_string", "args": [5]}
|
||||||
|
- data: '{"name": "user", "password": "123456"}'
|
||||||
|
- authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]}
|
||||||
|
- expected_status_code: 201
|
||||||
|
request:
|
||||||
|
url: http://127.0.0.1:5000/api/users/1000
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json
|
||||||
|
authorization: "${authorization}"
|
||||||
|
random: "${random}"
|
||||||
|
data: "${data}"
|
||||||
|
response:
|
||||||
|
status_code: "${expected_status_code}"
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json
|
||||||
|
body:
|
||||||
|
success: true
|
||||||
|
msg: user created successfully.
|
||||||
|
|
||||||
|
-
|
||||||
|
name: create user which does not exist
|
||||||
|
requires:
|
||||||
|
- random
|
||||||
|
- string
|
||||||
|
- hashlib
|
||||||
|
function_binds:
|
||||||
|
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
|
||||||
|
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||||
|
variable_binds:
|
||||||
|
- TOKEN: debugtalk
|
||||||
|
- random: {"func": "gen_random_string", "args": [5]}
|
||||||
|
- data: '{"name": "user", "password": "123456"}'
|
||||||
|
- authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]}
|
||||||
|
- expected_status_code: 500
|
||||||
|
request:
|
||||||
|
url: http://127.0.0.1:5000/api/users/1000
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json
|
||||||
|
authorization: "${authorization}"
|
||||||
|
random: "${random}"
|
||||||
|
data: "${data}"
|
||||||
|
response:
|
||||||
|
status_code: "${expected_status_code}"
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json
|
||||||
|
body:
|
||||||
|
success: true
|
||||||
|
msg: user created successfully.
|
||||||
@@ -45,3 +45,12 @@ class TestRunnerV2(ApiServerUnittest):
|
|||||||
result = self.test_runner.run_testcase_suite(testcases)
|
result = self.test_runner.run_testcase_suite(testcases)
|
||||||
self.assertEqual(len(result), 2)
|
self.assertEqual(len(result), 2)
|
||||||
self.assertEqual(result, [(True, {}), (True, {})])
|
self.assertEqual(result, [(True, {}), (True, {})])
|
||||||
|
|
||||||
|
def test_run_testcase_template_yaml(self):
|
||||||
|
testcase_file_path = os.path.join(
|
||||||
|
os.getcwd(), 'test/data/demo_template_separate.yml')
|
||||||
|
testcases = utils.load_testcases(testcase_file_path)
|
||||||
|
success, _ = self.test_runner.run_single_testcase(testcases[0])
|
||||||
|
self.assertTrue(success)
|
||||||
|
success, _ = self.test_runner.run_single_testcase(testcases[1])
|
||||||
|
self.assertFalse(success)
|
||||||
|
|||||||
Reference in New Issue
Block a user