change: use NewStep to construct a new test step

This commit is contained in:
debugtalk
2021-12-07 11:11:03 +08:00
parent 87f22e0a16
commit f7ab39374c
11 changed files with 47 additions and 42 deletions

View File

@@ -12,17 +12,17 @@ func TestBoomerStandaloneRun(t *testing.T) {
BaseURL: "http://httpbin.org",
},
TestSteps: []IStep{
Step("headers").
NewStep("headers").
GET("/headers").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
Step("user-agent").
NewStep("user-agent").
GET("/user-agent").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
Step("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}),
NewStep("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}),
},
}
testcase2 := &TestCasePath{demoTestCaseJSONPath}

View File

@@ -20,7 +20,7 @@ var demoTestCase = &hrp.TestCase{
},
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{ // step level variables
"n": 3, // inherit config level variables if not set in step level, a/varFoo1
"b": 34.5, // override config level variable if existed, n/b/varFoo2
@@ -37,7 +37,7 @@ var demoTestCase = &hrp.TestCase{
AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath
AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
hrp.Step("post json data").
hrp.NewStep("post json data").
POST("/post").
WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable
@@ -47,7 +47,7 @@ var demoTestCase = &hrp.TestCase{
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 5, "check args foo1").
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
hrp.Step("post form data").
hrp.NewStep("post form data").
POST("/post").
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
WithBody(map[string]interface{}{

View File

@@ -15,7 +15,7 @@ func TestCaseExtractStepSingle(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
@@ -52,7 +52,7 @@ func TestCaseExtractStepAssociation(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
@@ -71,7 +71,7 @@ func TestCaseExtractStepAssociation(t *testing.T) {
AssertEqual("$varFoo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
hrp.Step("post json data").
hrp.NewStep("post json data").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).

View File

@@ -19,7 +19,7 @@ func TestCaseCallFunction(t *testing.T) {
},
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
@@ -29,7 +29,7 @@ func TestCaseCallFunction(t *testing.T) {
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
AssertEqual("body.args.foo2", "12.3", "check args foo2"), // notice: request params value will be converted to string
hrp.Step("post json data with functions").
hrp.NewStep("post json data with functions").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).

View File

@@ -14,7 +14,7 @@ func TestCaseBasicRequest(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(map[string]string{
@@ -26,7 +26,7 @@ func TestCaseBasicRequest(t *testing.T) {
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2"),
hrp.Step("post raw text").
hrp.NewStep("post raw text").
POST("/post").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
@@ -36,7 +36,7 @@ func TestCaseBasicRequest(t *testing.T) {
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"),
hrp.Step("post form data").
hrp.NewStep("post form data").
POST("/post").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
@@ -47,7 +47,7 @@ func TestCaseBasicRequest(t *testing.T) {
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.form.foo1", "bar1", "check form foo1").
AssertEqual("body.form.foo2", "bar2", "check form foo2"),
hrp.Step("post json data").
hrp.NewStep("post json data").
POST("/post").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
@@ -57,7 +57,7 @@ func TestCaseBasicRequest(t *testing.T) {
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.json.foo1", "bar1", "check json foo1").
AssertEqual("body.json.foo2", "bar2", "check json foo2"),
hrp.Step("put request").
hrp.NewStep("put request").
PUT("/put").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",

View File

@@ -14,7 +14,7 @@ func TestCaseValidateStep(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
@@ -32,7 +32,7 @@ func TestCaseValidateStep(t *testing.T) {
AssertEqual("body.args.foo1", "bar1", "check args foo1"). // assert response json body with jmespath
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",

View File

@@ -19,7 +19,7 @@ func TestCaseConfigVariables(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "$agent"}).
@@ -47,7 +47,7 @@ func TestCaseStepVariables(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
@@ -85,7 +85,7 @@ func TestCaseOverrideConfigVariables(t *testing.T) {
Verify: false,
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1", // override config variable
"agent": "$agent", // reference config variable
@@ -125,7 +125,7 @@ func TestCaseParseVariables(t *testing.T) {
},
},
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"n": 3,
"b": 34.5,
@@ -140,7 +140,7 @@ func TestCaseParseVariables(t *testing.T) {
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
hrp.Step("post json data with functions").
hrp.NewStep("post json data with functions").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).

View File

@@ -20,17 +20,18 @@ import (
"github.com/httprunner/hrp/internal/ga"
)
// run API test with default configs
// Run starts to run API test with default configs.
func Run(testcases ...ITestCase) error {
t := &testing.T{}
return NewRunner(t).SetDebug(true).Run(testcases...)
}
func NewRunner(t *testing.T) *Runner {
// NewRunner constructs a new runner instance.
func NewRunner(t *testing.T) *runner {
if t == nil {
t = &testing.T{}
}
return &Runner{
return &runner{
t: t,
debug: false, // default to turn off debug
client: &http.Client{
@@ -43,20 +44,22 @@ func NewRunner(t *testing.T) *Runner {
}
}
type Runner struct {
type runner struct {
t *testing.T
debug bool
client *http.Client
sessionVariables map[string]interface{}
}
func (r *Runner) SetDebug(debug bool) *Runner {
// SetDebug configures whether to log HTTP request and response content.
func (r *runner) SetDebug(debug bool) *runner {
log.Info().Bool("debug", debug).Msg("[init] SetDebug")
r.debug = debug
return r
}
func (r *Runner) SetProxyUrl(proxyUrl string) *Runner {
// SetProxyUrl configures the proxy URL, which is usually used to capture HTTP packets for debugging.
func (r *runner) SetProxyUrl(proxyUrl string) *runner {
log.Info().Str("proxyUrl", proxyUrl).Msg("[init] SetProxyUrl")
p, err := url.Parse(proxyUrl)
if err != nil {
@@ -70,7 +73,8 @@ func (r *Runner) SetProxyUrl(proxyUrl string) *Runner {
return r
}
func (r *Runner) Run(testcases ...ITestCase) error {
// Run starts to execute one or multiple testcases.
func (r *runner) Run(testcases ...ITestCase) error {
event := ga.EventTracking{
Category: "RunAPITests",
Action: "hrp run",
@@ -94,7 +98,7 @@ func (r *Runner) Run(testcases ...ITestCase) error {
return nil
}
func (r *Runner) runCase(testcase *TestCase) error {
func (r *runner) runCase(testcase *TestCase) error {
config := &testcase.Config
if err := r.parseConfig(config); err != nil {
return err
@@ -113,7 +117,7 @@ func (r *Runner) runCase(testcase *TestCase) error {
return nil
}
func (r *Runner) runStep(step IStep, config *TConfig) (stepResult *stepData, err error) {
func (r *runner) runStep(step IStep, config *TConfig) (stepResult *stepData, err error) {
log.Info().Str("step", step.name()).Msg("run step start")
// copy step to avoid data racing
@@ -170,7 +174,7 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepResult *stepData, err
return
}
func (r *Runner) runStepRequest(step *TStep) (stepResult *stepData, err error) {
func (r *runner) runStepRequest(step *TStep) (stepResult *stepData, err error) {
stepResult = &stepData{
name: step.Name,
success: false,
@@ -335,7 +339,7 @@ func (r *Runner) runStepRequest(step *TStep) (stepResult *stepData, err error) {
return
}
func (r *Runner) runStepTestCase(step *TStep) (stepResult *stepData, err error) {
func (r *runner) runStepTestCase(step *TStep) (stepResult *stepData, err error) {
stepResult = &stepData{
name: step.Name,
success: false,
@@ -345,7 +349,7 @@ func (r *Runner) runStepTestCase(step *TStep) (stepResult *stepData, err error)
return
}
func (r *Runner) parseConfig(config *TConfig) error {
func (r *runner) parseConfig(config *TConfig) error {
// parse config variables
parsedVariables, err := parseVariables(config.Variables)
if err != nil {
@@ -371,7 +375,7 @@ func (r *Runner) parseConfig(config *TConfig) error {
return nil
}
func (r *Runner) GetSummary() *testCaseSummary {
func (r *runner) getSummary() *testCaseSummary {
return &testCaseSummary{}
}

View File

@@ -11,17 +11,17 @@ func TestHttpRunner(t *testing.T) {
BaseURL: "http://httpbin.org",
},
TestSteps: []IStep{
Step("headers").
NewStep("headers").
GET("/headers").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
Step("user-agent").
NewStep("user-agent").
GET("/user-agent").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
Step("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}),
NewStep("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}),
},
}
testcase2 := &TestCase{

View File

@@ -2,7 +2,8 @@ package hrp
import "fmt"
func Step(name string) *step {
// NewStep returns a new constructed teststep with specified step name.
func NewStep(name string) *step {
return &step{
TStep: &TStep{
Name: name,

View File

@@ -5,7 +5,7 @@ import (
)
var (
stepGET = Step("get with params").
stepGET = NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
@@ -16,7 +16,7 @@ var (
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check param foo1").
AssertEqual("body.args.foo2", "bar2", "check param foo2")
stepPOSTData = Step("post form data").
stepPOSTData = NewStep("post form data").
POST("/post").
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus", "Content-Type": "application/x-www-form-urlencoded"}).