mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
refactor: runStep
This commit is contained in:
@@ -29,6 +29,7 @@ func (b *Boomer) Run(testcases ...*TestCase) {
|
||||
}
|
||||
|
||||
func convertBoomerTask(testcase *TestCase) *boomer.Task {
|
||||
runner := NewRunner()
|
||||
return &boomer.Task{
|
||||
Name: testcase.Config.Name,
|
||||
Weight: testcase.Config.Weight,
|
||||
@@ -36,7 +37,8 @@ func convertBoomerTask(testcase *TestCase) *boomer.Task {
|
||||
config := &testcase.Config
|
||||
for _, step := range testcase.TestSteps {
|
||||
start := time.Now()
|
||||
err := step.Run(config)
|
||||
tStep := parseStep(step, config)
|
||||
err := runner.runStep(tStep)
|
||||
elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond)
|
||||
|
||||
if err == nil {
|
||||
|
||||
@@ -4,8 +4,7 @@ import "fmt"
|
||||
|
||||
// implements IStep interface
|
||||
type stepRequestExtraction struct {
|
||||
runner *Runner
|
||||
step *TStep
|
||||
step *TStep
|
||||
}
|
||||
|
||||
func (s *stepRequestExtraction) WithJmesPath(jmesPath string, varName string) *stepRequestExtraction {
|
||||
@@ -27,6 +26,6 @@ func (s *stepRequestExtraction) Type() string {
|
||||
return fmt.Sprintf("request-%v", s.step.Request.Method)
|
||||
}
|
||||
|
||||
func (s *stepRequestExtraction) Run(config *TConfig) error {
|
||||
return s.runner.runStep(s.step)
|
||||
func (s *stepRequestExtraction) ToStruct() *TStep {
|
||||
return s.step
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ type TStep struct {
|
||||
type IStep interface {
|
||||
Name() string
|
||||
Type() string
|
||||
Run(config *TConfig) error
|
||||
ToStruct() *TStep
|
||||
}
|
||||
|
||||
type TestCase struct {
|
||||
|
||||
14
runner.go
14
runner.go
@@ -1,6 +1,7 @@
|
||||
package httpboomer
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/imroc/req"
|
||||
@@ -33,15 +34,24 @@ func (r *Runner) Run(testcases ...*TestCase) error {
|
||||
|
||||
func (r *Runner) runCase(testcase *TestCase) error {
|
||||
config := &testcase.Config
|
||||
log.Printf("Start to run testcase: %v", config.Name)
|
||||
for _, step := range testcase.TestSteps {
|
||||
if err := step.Run(config); err != nil {
|
||||
tStep := parseStep(step, config)
|
||||
if err := r.runStep(tStep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseStep(step IStep, config *TConfig) *TStep {
|
||||
tStep := step.ToStruct()
|
||||
tStep.Request.URL = config.BaseURL + tStep.Request.URL
|
||||
return tStep
|
||||
}
|
||||
|
||||
func (r *Runner) runStep(step *TStep) error {
|
||||
log.Printf("run step begin: %v >>>>>>", step.Name)
|
||||
var v []interface{}
|
||||
v = append(v, req.Header(step.Request.Headers))
|
||||
v = append(v, req.Param(step.Request.Params))
|
||||
@@ -53,11 +63,13 @@ func (r *Runner) runStep(step *TStep) error {
|
||||
})
|
||||
}
|
||||
|
||||
req.Debug = true
|
||||
resp, err := r.Client.Do(string(step.Request.Method), step.Request.URL, v...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Response().Body.Close()
|
||||
log.Printf("run step end: %v <<<<<<\n", step.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
52
step.go
52
step.go
@@ -4,7 +4,6 @@ import "fmt"
|
||||
|
||||
func Step(name string) *step {
|
||||
return &step{
|
||||
runner: defaultRunner,
|
||||
TStep: &TStep{
|
||||
Name: name,
|
||||
Request: &TRequest{},
|
||||
@@ -14,16 +13,9 @@ func Step(name string) *step {
|
||||
}
|
||||
|
||||
type step struct {
|
||||
runner *Runner
|
||||
config *TConfig
|
||||
*TStep
|
||||
}
|
||||
|
||||
func (s *step) WithRunner(runner *Runner) *step {
|
||||
s.runner = runner
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *step) WithVariables(variables map[string]interface{}) *step {
|
||||
s.TStep.Variables = variables
|
||||
return s
|
||||
@@ -38,8 +30,7 @@ func (s *step) GET(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = GET
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,8 +38,7 @@ func (s *step) HEAD(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = HEAD
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +46,7 @@ func (s *step) POST(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = POST
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +54,7 @@ func (s *step) PUT(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = PUT
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,8 +62,7 @@ func (s *step) DELETE(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = DELETE
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,8 +70,7 @@ func (s *step) OPTIONS(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = OPTIONS
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,8 +78,7 @@ func (s *step) PATCH(url string) *requestWithOptionalArgs {
|
||||
s.TStep.Request.Method = PATCH
|
||||
s.TStep.Request.URL = url
|
||||
return &requestWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,15 +86,13 @@ func (s *step) PATCH(url string) *requestWithOptionalArgs {
|
||||
func (s *step) CallRefCase(tc *TestCase) *testcaseWithOptionalArgs {
|
||||
s.TStep.TestCase = tc
|
||||
return &testcaseWithOptionalArgs{
|
||||
runner: s.runner,
|
||||
step: s.TStep,
|
||||
step: s.TStep,
|
||||
}
|
||||
}
|
||||
|
||||
// implements IStep interface
|
||||
type requestWithOptionalArgs struct {
|
||||
runner *Runner
|
||||
step *TStep
|
||||
step *TStep
|
||||
}
|
||||
|
||||
func (s *requestWithOptionalArgs) SetVerify(verify bool) *requestWithOptionalArgs {
|
||||
@@ -169,15 +152,13 @@ func (s *requestWithOptionalArgs) TeardownHook(hook string) *requestWithOptional
|
||||
|
||||
func (s *requestWithOptionalArgs) Validate() *stepRequestValidation {
|
||||
return &stepRequestValidation{
|
||||
runner: s.runner,
|
||||
step: s.step,
|
||||
step: s.step,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *requestWithOptionalArgs) Extract() *stepRequestExtraction {
|
||||
return &stepRequestExtraction{
|
||||
runner: s.runner,
|
||||
step: s.step,
|
||||
step: s.step,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,14 +170,13 @@ func (s *requestWithOptionalArgs) Type() string {
|
||||
return fmt.Sprintf("request-%v", s.step.Request.Method)
|
||||
}
|
||||
|
||||
func (s *requestWithOptionalArgs) Run(config *TConfig) error {
|
||||
return s.runner.runStep(s.step)
|
||||
func (s *requestWithOptionalArgs) ToStruct() *TStep {
|
||||
return s.step
|
||||
}
|
||||
|
||||
// implements IStep interface
|
||||
type testcaseWithOptionalArgs struct {
|
||||
runner *Runner
|
||||
step *TStep
|
||||
step *TStep
|
||||
}
|
||||
|
||||
func (s *testcaseWithOptionalArgs) TeardownHook(hook string) *testcaseWithOptionalArgs {
|
||||
@@ -217,6 +197,6 @@ func (s *testcaseWithOptionalArgs) Type() string {
|
||||
return "testcase"
|
||||
}
|
||||
|
||||
func (s *testcaseWithOptionalArgs) Run(config *TConfig) error {
|
||||
return s.runner.runCase(s.step.TestCase)
|
||||
func (s *testcaseWithOptionalArgs) ToStruct() *TStep {
|
||||
return s.step
|
||||
}
|
||||
|
||||
20
step_test.go
20
step_test.go
@@ -6,14 +6,14 @@ import (
|
||||
|
||||
var (
|
||||
stepGET = Step("get with params").
|
||||
GET("https://postman-echo.com/get").
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}).
|
||||
WithCookies(map[string]string{"user": "debugtalk"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code")
|
||||
stepPOSTData = Step("post form data").
|
||||
POST("https://postman-echo.com/post").
|
||||
POST("/post").
|
||||
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}).
|
||||
WithData("a=1&b=2").
|
||||
@@ -27,8 +27,8 @@ func TestRunRequestGetToStruct(t *testing.T) {
|
||||
if tStep.Request.Method != GET {
|
||||
t.Fatalf("tStep.Request.Method != GET")
|
||||
}
|
||||
if tStep.Request.URL != "https://postman-echo.com/get" {
|
||||
t.Fatalf("tStep.Request.URL != 'https://postman-echo.com/get'")
|
||||
if tStep.Request.URL != "/get" {
|
||||
t.Fatalf("tStep.Request.URL != '/get'")
|
||||
}
|
||||
if tStep.Request.Params["foo1"] != "bar1" || tStep.Request.Params["foo2"] != "bar2" {
|
||||
t.Fatalf("tStep.Request.Params mismatch")
|
||||
@@ -49,8 +49,8 @@ func TestRunRequestPostDataToStruct(t *testing.T) {
|
||||
if tStep.Request.Method != POST {
|
||||
t.Fatalf("tStep.Request.Method != POST")
|
||||
}
|
||||
if tStep.Request.URL != "https://postman-echo.com/post" {
|
||||
t.Fatalf("tStep.Request.URL != 'https://postman-echo.com/post'")
|
||||
if tStep.Request.URL != "/post" {
|
||||
t.Fatalf("tStep.Request.URL != '/post'")
|
||||
}
|
||||
if tStep.Request.Params["foo1"] != "bar1" || tStep.Request.Params["foo2"] != "bar2" {
|
||||
t.Fatalf("tStep.Request.Params mismatch")
|
||||
@@ -70,11 +70,13 @@ func TestRunRequestPostDataToStruct(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunRequestRun(t *testing.T) {
|
||||
config := &TConfig{}
|
||||
if err := stepGET.Run(config); err != nil {
|
||||
config := &TConfig{
|
||||
BaseURL: "https://postman-echo.com",
|
||||
}
|
||||
if err := defaultRunner.runStep(parseStep(stepGET, config)); err != nil {
|
||||
t.Fatalf("tStep.Run() error: %s", err)
|
||||
}
|
||||
if err := stepPOSTData.Run(config); err != nil {
|
||||
if err := defaultRunner.runStep(parseStep(stepPOSTData, config)); err != nil {
|
||||
t.Fatalf("tStepPOSTData.Run() error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,7 @@ import "fmt"
|
||||
|
||||
// implements IStep interface
|
||||
type stepRequestValidation struct {
|
||||
runner *Runner
|
||||
step *TStep
|
||||
step *TStep
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation {
|
||||
@@ -27,6 +26,6 @@ func (s *stepRequestValidation) Type() string {
|
||||
return fmt.Sprintf("request-%v", s.step.Request.Method)
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) Run(config *TConfig) error {
|
||||
return s.runner.runStep(s.step)
|
||||
func (s *stepRequestValidation) ToStruct() *TStep {
|
||||
return s.step
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user