feat: run specified YAML/JSON testcase file

This commit is contained in:
debugtalk
2020-05-15 20:58:53 +08:00
parent 2cd9eba6a3
commit 907512e214
4 changed files with 53 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ from httprunner.runner import HttpRunner
from httprunner.schema import ProjectMeta, TestCase from httprunner.schema import ProjectMeta, TestCase
router = APIRouter() router = APIRouter()
runner = HttpRunner()
@router.post("/hrun/debug/testcase", tags=["debug"]) @router.post("/hrun/debug/testcase", tags=["debug"])
@@ -19,9 +20,7 @@ async def debug_single_testcase(project_meta: ProjectMeta, testcase: TestCase):
for func_name in new_added_keys: for func_name in new_added_keys:
project_meta.functions[func_name] = locals()[func_name] project_meta.functions[func_name] = locals()[func_name]
config = testcase.config runner.with_project_meta(project_meta).run(testcase)
teststeps = testcase.teststeps
runner = HttpRunner(config, teststeps).with_project_meta(project_meta).run()
summary = runner.get_summary() summary = runner.get_summary()
if not summary.success: if not summary.success:

View File

@@ -1,11 +1,11 @@
import os import os
import time import time
from datetime import datetime from datetime import datetime
from typing import List, Dict from typing import List, Dict, Text
from loguru import logger from loguru import logger
from httprunner import utils from httprunner import utils, exceptions
from httprunner.client import HttpSession from httprunner.client import HttpSession
from httprunner.exceptions import ValidationFailure, ParamsError from httprunner.exceptions import ValidationFailure, ParamsError
from httprunner.loader import load_project_meta, load_testcase_file from httprunner.loader import load_project_meta, load_testcase_file
@@ -28,9 +28,9 @@ class HttpRunner(object):
config: TConfig config: TConfig
teststeps: List[TStep] teststeps: List[TStep]
session: HttpSession session: HttpSession = None
variables: VariablesMapping = {} variables: VariablesMapping = {}
step_datas: List[StepData] = [] step_datas: List[StepData] = None
validation_results: Dict = {} validation_results: Dict = {}
session_variables: Dict = {} session_variables: Dict = {}
success: bool = True # indicate testcase execution result success: bool = True # indicate testcase execution result
@@ -132,13 +132,11 @@ class HttpRunner(object):
step_variables = step.variables step_variables = step.variables
ref_testcase_path = os.path.join(self.project_meta.PWD, step.testcase) ref_testcase_path = os.path.join(self.project_meta.PWD, step.testcase)
_, testcase_obj = load_testcase_file(ref_testcase_path)
case_result = ( case_result = (
HttpRunner() HttpRunner()
.with_session(self.session) .with_session(self.session)
.with_variables(step_variables) .with_variables(step_variables)
.run(testcase_obj) .run_path(ref_testcase_path)
) )
step_data.data = case_result.step_datas # list of step data step_data.data = case_result.step_datas # list of step data
step_data.export = case_result.get_export_variables() step_data.export = case_result.get_export_variables()
@@ -175,7 +173,7 @@ class HttpRunner(object):
self.project_meta = ProjectMeta() self.project_meta = ProjectMeta()
self.start_at = time.time() self.start_at = time.time()
self.step_datas.clear() self.step_datas: List[StepData] = []
self.session_variables.clear() self.session_variables.clear()
for step in self.teststeps: for step in self.teststeps:
# update with config variables # update with config variables
@@ -194,6 +192,13 @@ class HttpRunner(object):
self.duration = time.time() - self.start_at self.duration = time.time() - self.start_at
return self return self
def run_path(self, path: Text) -> "HttpRunner":
if not os.path.isfile(path):
raise exceptions.ParamsError(f"Invalid testcase path: {path}")
_, testcase_obj = load_testcase_file(path)
return self.run(testcase_obj)
def get_export_variables(self) -> Dict: def get_export_variables(self) -> Dict:
export_vars_mapping = {} export_vars_mapping = {}
for var_name in self.config.export: for var_name in self.config.export:

View File

@@ -0,0 +1,36 @@
import unittest
from httprunner.runner import HttpRunner
class TestHttpRunner(unittest.TestCase):
def setUp(self):
self.runner = HttpRunner()
def test_run_testcase_by_path_request_only(self):
self.runner.run_path(
"examples/postman_echo/request_methods/request_with_variables.yml"
)
result = self.runner.get_summary()
self.assertTrue(result.success)
self.assertEqual(
result.name, "request methods testcase with variables"
)
self.assertEqual(
result.step_datas[0].name, "get with params"
)
self.assertEqual(len(result.step_datas), 3)
def test_run_testcase_by_path_ref_testcase(self):
self.runner.run_path(
"examples/postman_echo/request_methods/request_with_testcase_reference.yml"
)
result = self.runner.get_summary()
self.assertTrue(result.success)
self.assertEqual(
result.name, "request methods testcase: reference testcase"
)
self.assertEqual(
result.step_datas[0].name, "request with variables"
)
self.assertEqual(len(result.step_datas), 1)

View File

@@ -2,7 +2,7 @@ import io
import os import os
import unittest import unittest
from httprunner import new_loader, utils from httprunner import loader, utils
class TestUtils(unittest.TestCase): class TestUtils(unittest.TestCase):
@@ -16,7 +16,7 @@ class TestUtils(unittest.TestCase):
def current_validators(self): def current_validators(self):
from httprunner.builtin import comparators from httprunner.builtin import comparators
functions_mapping = new_loader.load_module_functions(comparators) functions_mapping = loader.load_module_functions(comparators)
functions_mapping["equals"](None, None) functions_mapping["equals"](None, None)
functions_mapping["equals"](1, 1) functions_mapping["equals"](1, 1)