diff --git a/boomer_test.go b/boomer_test.go index 5d2e18a2..bc587297 100644 --- a/boomer_test.go +++ b/boomer_test.go @@ -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} diff --git a/examples/demo_test.go b/examples/demo_test.go index 22fd2f77..768ed470 100644 --- a/examples/demo_test.go +++ b/examples/demo_test.go @@ -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{}{ diff --git a/examples/extract_test.go b/examples/extract_test.go index 7f3891bf..b5555663 100644 --- a/examples/extract_test.go +++ b/examples/extract_test.go @@ -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"}). diff --git a/examples/function_test.go b/examples/function_test.go index 2ae4d642..946c8a1b 100644 --- a/examples/function_test.go +++ b/examples/function_test.go @@ -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)}"}). diff --git a/examples/request_test.go b/examples/request_test.go index a17047a5..43a8f417 100644 --- a/examples/request_test.go +++ b/examples/request_test.go @@ -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", diff --git a/examples/validate_test.go b/examples/validate_test.go index 17f768ca..e1ddb45e 100644 --- a/examples/validate_test.go +++ b/examples/validate_test.go @@ -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", diff --git a/examples/variables_test.go b/examples/variables_test.go index 2d4f4c55..7fc70bac 100644 --- a/examples/variables_test.go +++ b/examples/variables_test.go @@ -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)}"}). diff --git a/runner.go b/runner.go index e143b6d8..dc9206ff 100644 --- a/runner.go +++ b/runner.go @@ -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{} } diff --git a/runner_test.go b/runner_test.go index 72476dde..cd48eec4 100644 --- a/runner_test.go +++ b/runner_test.go @@ -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{ diff --git a/step.go b/step.go index d86fa897..53d35c21 100644 --- a/step.go +++ b/step.go @@ -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, diff --git a/step_test.go b/step_test.go index 40206391..fc7db698 100644 --- a/step_test.go +++ b/step_test.go @@ -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"}).