mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-13 00:11:28 +08:00
optimize Runner initialization
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
from ate import exception, response
|
from ate import exception, response
|
||||||
from ate.client import HttpSession
|
from ate.client import HttpSession
|
||||||
from ate.context import Context
|
from ate.context import Context
|
||||||
@@ -5,8 +7,8 @@ from ate.context import Context
|
|||||||
|
|
||||||
class Runner(object):
|
class Runner(object):
|
||||||
|
|
||||||
def __init__(self, base_url=None):
|
def __init__(self, http_client_session=None):
|
||||||
self.client = HttpSession(base_url)
|
self.http_client_session = http_client_session
|
||||||
self.context = Context()
|
self.context = Context()
|
||||||
|
|
||||||
def init_config(self, config_dict, level):
|
def init_config(self, config_dict, level):
|
||||||
@@ -48,7 +50,10 @@ class Runner(object):
|
|||||||
request_config = config_dict.get('request', {})
|
request_config = config_dict.get('request', {})
|
||||||
if level == "testset":
|
if level == "testset":
|
||||||
base_url = request_config.pop("base_url", None)
|
base_url = request_config.pop("base_url", None)
|
||||||
self.client = HttpSession(base_url)
|
self.http_client_session = self.http_client_session or HttpSession(base_url)
|
||||||
|
else:
|
||||||
|
# testcase
|
||||||
|
self.http_client_session = self.http_client_session or requests.Session()
|
||||||
self.context.register_request(request_config, level)
|
self.context.register_request(request_config, level)
|
||||||
|
|
||||||
def run_test(self, testcase):
|
def run_test(self, testcase):
|
||||||
@@ -84,7 +89,7 @@ class Runner(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
raise exception.ParamsError("URL or METHOD missed!")
|
raise exception.ParamsError("URL or METHOD missed!")
|
||||||
|
|
||||||
resp = self.client.request(url=url, method=method, **parsed_request)
|
resp = self.http_client_session.request(url=url, method=method, **parsed_request)
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
|
||||||
extract_binds = testcase.get("extract_binds", {})
|
extract_binds = testcase.get("extract_binds", {})
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ from test.base import ApiServerUnittest
|
|||||||
class TestRunner(ApiServerUnittest):
|
class TestRunner(ApiServerUnittest):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
base_url = "http://127.0.0.1:5000"
|
self.test_runner = runner.Runner()
|
||||||
self.test_runner = runner.Runner(base_url)
|
|
||||||
self.clear_users()
|
self.clear_users()
|
||||||
|
|
||||||
def clear_users(self):
|
def clear_users(self):
|
||||||
@@ -32,7 +31,7 @@ class TestRunner(ApiServerUnittest):
|
|||||||
testcase = {
|
testcase = {
|
||||||
"name": "create user which does not exist",
|
"name": "create user which does not exist",
|
||||||
"request": {
|
"request": {
|
||||||
"url": "/api/users/1000",
|
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"headers": {
|
"headers": {
|
||||||
"content-type": "application/json"
|
"content-type": "application/json"
|
||||||
|
|||||||
Reference in New Issue
Block a user