mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 19:39:44 +08:00
refactor: runner api
This commit is contained in:
@@ -47,7 +47,7 @@ func (b *Boomer) Run(testcases ...ITestCase) {
|
||||
}
|
||||
|
||||
func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
|
||||
runner := NewRunner().SetDebug(b.debug)
|
||||
runner := NewRunner(nil).SetDebug(b.debug)
|
||||
return &boomer.Task{
|
||||
Name: testcase.Config.Name,
|
||||
Weight: testcase.Config.Weight,
|
||||
|
||||
@@ -22,4 +22,4 @@ Copyright 2021 debugtalk
|
||||
* [hrp har2case](hrp_har2case.md) - Convert HAR to json/yaml testcase files
|
||||
* [hrp run](hrp_run.md) - run API test
|
||||
|
||||
###### Auto generated by spf13/cobra on 17-Oct-2021
|
||||
###### Auto generated by spf13/cobra on 11-Nov-2021
|
||||
|
||||
@@ -37,4 +37,4 @@ hrp boom [flags]
|
||||
|
||||
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.
|
||||
|
||||
###### Auto generated by spf13/cobra on 17-Oct-2021
|
||||
###### Auto generated by spf13/cobra on 11-Nov-2021
|
||||
|
||||
@@ -23,4 +23,4 @@ hrp har2case harPath... [flags]
|
||||
|
||||
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.
|
||||
|
||||
###### Auto generated by spf13/cobra on 17-Oct-2021
|
||||
###### Auto generated by spf13/cobra on 11-Nov-2021
|
||||
|
||||
@@ -30,4 +30,4 @@ hrp run path... [flags]
|
||||
|
||||
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing.
|
||||
|
||||
###### Auto generated by spf13/cobra on 17-Oct-2021
|
||||
###### Auto generated by spf13/cobra on 11-Nov-2021
|
||||
|
||||
@@ -79,7 +79,7 @@ func TestGenDemoTestCase(t *testing.T) {
|
||||
}
|
||||
|
||||
func Example_TestCase() {
|
||||
err := hrp.NewRunner().Run(demoTestCase)
|
||||
err := hrp.NewRunner(nil).Run(demoTestCase) // hrp.Run(demoTestCase)
|
||||
fmt.Println(err)
|
||||
// Output:
|
||||
// <nil>
|
||||
@@ -87,7 +87,7 @@ func Example_TestCase() {
|
||||
|
||||
func Example_JSONTestCase() {
|
||||
testCase := &hrp.TestCasePath{Path: demoTestCaseJSONPath}
|
||||
err := hrp.NewRunner().Run(testCase)
|
||||
err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase)
|
||||
fmt.Println(err)
|
||||
// Output:
|
||||
// <nil>
|
||||
@@ -95,7 +95,7 @@ func Example_JSONTestCase() {
|
||||
|
||||
func Example_YAMTestCase() {
|
||||
testCase := &hrp.TestCasePath{Path: demoTestCaseYAMLPath}
|
||||
err := hrp.NewRunner().Run(testCase)
|
||||
err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase)
|
||||
fmt.Println(err)
|
||||
// Output:
|
||||
// <nil>
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestCaseExtractStepSingle(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func TestCaseExtractStepAssociation(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestCaseCallFunction(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ func TestCaseBasicRequest(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestCaseValidateStep(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestCaseConfigVariables(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func TestCaseStepVariables(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func TestCaseOverrideConfigVariables(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func TestCaseParseVariables(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.Run(t, testcase)
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ var runCmd = &cobra.Command{
|
||||
for _, arg := range args {
|
||||
paths = append(paths, &hrp.TestCasePath{Path: arg})
|
||||
}
|
||||
runner := hrp.NewRunner().SetDebug(!silentFlag)
|
||||
runner := hrp.NewRunner(nil).SetDebug(!silentFlag)
|
||||
if proxyUrl != "" {
|
||||
runner.SetProxyUrl(proxyUrl)
|
||||
}
|
||||
|
||||
18
runner.go
18
runner.go
@@ -18,13 +18,17 @@ import (
|
||||
)
|
||||
|
||||
// run API test with default configs
|
||||
func Run(t *testing.T, testcases ...ITestCase) error {
|
||||
return NewRunner().WithTestingT(t).SetDebug(true).Run(testcases...)
|
||||
func Run(testcases ...ITestCase) error {
|
||||
t := &testing.T{}
|
||||
return NewRunner(t).SetDebug(true).Run(testcases...)
|
||||
}
|
||||
|
||||
func NewRunner() *Runner {
|
||||
func NewRunner(t *testing.T) *Runner {
|
||||
if t == nil {
|
||||
t = &testing.T{}
|
||||
}
|
||||
return &Runner{
|
||||
t: &testing.T{},
|
||||
t: t,
|
||||
debug: false, // default to turn off debug
|
||||
client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
@@ -41,12 +45,6 @@ type Runner struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (r *Runner) WithTestingT(t *testing.T) *Runner {
|
||||
log.Info().Msg("[init] WithTestingT")
|
||||
r.t = t
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Runner) SetDebug(debug bool) *Runner {
|
||||
log.Info().Bool("debug", debug).Msg("[init] SetDebug")
|
||||
r.debug = debug
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestHttpRunner(t *testing.T) {
|
||||
}
|
||||
testcase3 := &TestCasePath{demoTestCaseJSONPath}
|
||||
|
||||
err := Run(t, testcase1, testcase2, testcase3)
|
||||
err := NewRunner(t).Run(testcase1, testcase2, testcase3)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func TestRunRequestRun(t *testing.T) {
|
||||
config := &TConfig{
|
||||
BaseURL: "https://postman-echo.com",
|
||||
}
|
||||
runner := NewRunner().SetDebug(true).WithTestingT(t)
|
||||
runner := NewRunner(t).SetDebug(true)
|
||||
if _, err := runner.runStep(stepGET, config); err != nil {
|
||||
t.Fatalf("tStep.Run() error: %s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user