mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
refactor: IStep interface
This commit is contained in:
@@ -32,11 +32,10 @@ func convertBoomerTask(testcase *TestCase) *boomer.Task {
|
||||
err := step.Run()
|
||||
elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond)
|
||||
|
||||
tStep := step.ToStruct()
|
||||
if err == nil {
|
||||
boomer.RecordSuccess(string(tStep.Request.Method), tStep.Name, elapsed, int64(0))
|
||||
boomer.RecordSuccess(step.Type(), step.Name(), elapsed, int64(0))
|
||||
} else {
|
||||
boomer.RecordFailure(string(tStep.Request.Method), tStep.Name, elapsed, err.Error())
|
||||
boomer.RecordFailure(step.Type(), step.Name(), elapsed, err.Error())
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
12
extract.go
12
extract.go
@@ -1,5 +1,7 @@
|
||||
package httpboomer
|
||||
|
||||
import "fmt"
|
||||
|
||||
// implements IStep interface
|
||||
type stepRequestExtraction struct {
|
||||
runner *Runner
|
||||
@@ -13,12 +15,16 @@ func (s *stepRequestExtraction) WithJmesPath(jmesPath string, varName string) *s
|
||||
|
||||
func (s *stepRequestExtraction) Validate() *stepRequestValidation {
|
||||
return &stepRequestValidation{
|
||||
TStep: s.step,
|
||||
step: s.step,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *stepRequestExtraction) ToStruct() *TStep {
|
||||
return s.step
|
||||
func (s *stepRequestExtraction) Name() string {
|
||||
return s.step.Name
|
||||
}
|
||||
|
||||
func (s *stepRequestExtraction) Type() string {
|
||||
return fmt.Sprintf("request-%v", s.step.Request.Method)
|
||||
}
|
||||
|
||||
func (s *stepRequestExtraction) Run() error {
|
||||
|
||||
@@ -61,7 +61,8 @@ type TStep struct {
|
||||
|
||||
// interface for all types of steps
|
||||
type IStep interface {
|
||||
ToStruct() *TStep
|
||||
Name() string
|
||||
Type() string
|
||||
Run() error
|
||||
}
|
||||
|
||||
|
||||
20
step.go
20
step.go
@@ -1,5 +1,7 @@
|
||||
package httpboomer
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Step(name string) *step {
|
||||
return &step{
|
||||
runner: defaultRunner,
|
||||
@@ -167,7 +169,7 @@ func (s *requestWithOptionalArgs) TeardownHook(hook string) *requestWithOptional
|
||||
func (s *requestWithOptionalArgs) Validate() *stepRequestValidation {
|
||||
return &stepRequestValidation{
|
||||
runner: s.runner,
|
||||
TStep: s.step,
|
||||
step: s.step,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,8 +180,12 @@ func (s *requestWithOptionalArgs) Extract() *stepRequestExtraction {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *requestWithOptionalArgs) ToStruct() *TStep {
|
||||
return s.step
|
||||
func (s *requestWithOptionalArgs) Name() string {
|
||||
return s.step.Name
|
||||
}
|
||||
|
||||
func (s *requestWithOptionalArgs) Type() string {
|
||||
return fmt.Sprintf("request-%v", s.step.Request.Method)
|
||||
}
|
||||
|
||||
func (s *requestWithOptionalArgs) Run() error {
|
||||
@@ -202,8 +208,12 @@ func (s *testcaseWithOptionalArgs) Export(names ...string) *testcaseWithOptional
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *testcaseWithOptionalArgs) ToStruct() *TStep {
|
||||
return s.step
|
||||
func (s *testcaseWithOptionalArgs) Name() string {
|
||||
return s.step.Name
|
||||
}
|
||||
|
||||
func (s *testcaseWithOptionalArgs) Type() string {
|
||||
return "testcase"
|
||||
}
|
||||
|
||||
func (s *testcaseWithOptionalArgs) Run() error {
|
||||
|
||||
@@ -23,7 +23,7 @@ var (
|
||||
)
|
||||
|
||||
func TestRunRequestGetToStruct(t *testing.T) {
|
||||
tStep := stepGET.ToStruct()
|
||||
tStep := stepGET.step
|
||||
if tStep.Request.Method != GET {
|
||||
t.Fatalf("tStep.Request.Method != GET")
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func TestRunRequestGetToStruct(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunRequestPostDataToStruct(t *testing.T) {
|
||||
tStep := stepPOSTData.ToStruct()
|
||||
tStep := stepPOSTData.step
|
||||
if tStep.Request.Method != POST {
|
||||
t.Fatalf("tStep.Request.Method != POST")
|
||||
}
|
||||
|
||||
16
validate.go
16
validate.go
@@ -1,9 +1,11 @@
|
||||
package httpboomer
|
||||
|
||||
import "fmt"
|
||||
|
||||
// implements IStep interface
|
||||
type stepRequestValidation struct {
|
||||
runner *Runner
|
||||
TStep *TStep
|
||||
step *TStep
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation {
|
||||
@@ -13,14 +15,18 @@ func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{
|
||||
Expect: expected,
|
||||
Message: msg,
|
||||
}
|
||||
s.TStep.Validators = append(s.TStep.Validators, validator)
|
||||
s.step.Validators = append(s.step.Validators, validator)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) ToStruct() *TStep {
|
||||
return s.TStep
|
||||
func (s *stepRequestValidation) Name() string {
|
||||
return s.step.Name
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) Type() string {
|
||||
return fmt.Sprintf("request-%v", s.step.Request.Method)
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) Run() error {
|
||||
return s.runner.runStep(s.TStep)
|
||||
return s.runner.runStep(s.step)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user