feat: describe testcase in chain-call style, run testcase

This commit is contained in:
debugtalk
2020-06-01 18:56:54 +08:00
parent 5b0bc40786
commit 1b7f9b334b
3 changed files with 32 additions and 24 deletions

View File

@@ -6,7 +6,7 @@ import sys
sys.path.insert(0, os.getcwd())
from httprunner import HttpRunner, TConfig, TStep
from httprunner import HttpRunner, Config, Step
from examples.postman_echo.request_methods.request_with_functions_test import (
TestCaseRequestWithFunctions as RequestWithFunctions,
@@ -14,24 +14,17 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
class TestCaseRequestWithTestcaseReference(HttpRunner):
config = TConfig(
**{
"name": "request methods testcase: reference testcase",
"variables": {"foo1": "session_bar1"},
"base_url": "https://postman-echo.com",
"verify": False,
"path": "examples/postman_echo/request_methods/request_with_testcase_reference_test.py",
}
config = (
Config("request methods testcase: reference testcase")
.variables(foo1="session_bar1")
.base_url("https://postman-echo.com")
.verify(False)
)
teststeps = [
TStep(
**{
"name": "request with functions",
"variables": {"foo1": "override_bar1"},
"testcase": RequestWithFunctions,
}
),
Step("request with functions")
.with_variables(foo1="override_bar1")
.run_testcase(RequestWithFunctions),
]

View File

@@ -61,8 +61,8 @@ class TRequest(BaseModel):
class TStep(BaseModel):
name: Name
request: TRequest = None
testcase: Union[Text, Callable] = ""
request: Union[TRequest, None] = None
testcase: Union[Text, Callable, None] = None
variables: VariablesMapping = {}
setup_hooks: Hook = []
teardown_hooks: Hook = []

View File

@@ -1,11 +1,12 @@
import inspect
from typing import Text, Any, Dict
from typing import Text, Any, Dict, Callable
from httprunner.schema import (
TConfig,
TStep,
TRequest,
MethodEnum,
TestCase,
)
@@ -128,18 +129,28 @@ class Request(object):
class StepValidation(object):
def __init__(
self, name: Text, variables: Dict, extractors: Dict, request: TRequest
self,
name: Text,
variables: Dict,
extractors: Dict,
request: TRequest = None,
testcase: Callable = None,
):
self.__name = name
self.__variables = variables
self.__extractors = extractors
self.__request = request
self.__request: TRequest = request
self.__testcase: Callable = testcase
self.__validators = []
@property
def request(self) -> TRequest:
return self.__request
@property
def testcase(self) -> TestCase:
return self.__testcase
def assert_equal(self, jmes_path: Text, expected_value: Any) -> "StepValidation":
self.__validators.append({"eq": [jmes_path, expected_value]})
return self
@@ -161,6 +172,7 @@ class StepValidation(object):
name=self.__name,
variables=self.__variables,
request=self.__request,
testcase=self.__testcase,
extract=self.__extractors,
validate=self.__validators,
)
@@ -171,7 +183,6 @@ class Step(object):
self.__name = name
self.__variables = {}
self.__extractors = {}
self.__request = None
def with_variables(self, **variables) -> "Step":
self.__variables.update(variables)
@@ -182,7 +193,11 @@ class Step(object):
return self
def run_request(self, req_obj: RequestWithOptionalArgs) -> "StepValidation":
self.__request = req_obj.perform()
return StepValidation(
self.__name, self.__variables, self.__extractors, self.__request
self.__name, self.__variables, self.__extractors, request=req_obj.perform()
)
def run_testcase(self, testcase: Callable) -> "StepValidation":
return StepValidation(
self.__name, self.__variables, self.__extractors, testcase=testcase
)