mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
refactor: NewConfig
This commit is contained in:
@@ -58,7 +58,7 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
|
|||||||
Weight: testcase.Config.Weight,
|
Weight: testcase.Config.Weight,
|
||||||
Fn: func() {
|
Fn: func() {
|
||||||
runner := NewRunner(nil).SetDebug(b.debug)
|
runner := NewRunner(nil).SetDebug(b.debug)
|
||||||
config := &testcase.Config
|
config := testcase.Config
|
||||||
for _, step := range testcase.TestSteps {
|
for _, step := range testcase.TestSteps {
|
||||||
var err error
|
var err error
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|||||||
@@ -7,10 +7,7 @@ import (
|
|||||||
|
|
||||||
func TestBoomerStandaloneRun(t *testing.T) {
|
func TestBoomerStandaloneRun(t *testing.T) {
|
||||||
testcase1 := &TestCase{
|
testcase1 := &TestCase{
|
||||||
Config: TConfig{
|
Config: NewConfig("TestCase1").SetBaseURL("http://httpbin.org"),
|
||||||
Name: "TestCase1",
|
|
||||||
BaseURL: "http://httpbin.org",
|
|
||||||
},
|
|
||||||
TestSteps: []IStep{
|
TestSteps: []IStep{
|
||||||
NewStep("headers").
|
NewStep("headers").
|
||||||
GET("/headers").
|
GET("/headers").
|
||||||
@@ -22,7 +19,7 @@ func TestBoomerStandaloneRun(t *testing.T) {
|
|||||||
Validate().
|
Validate().
|
||||||
AssertEqual("status_code", 200, "check status code").
|
AssertEqual("status_code", 200, "check status code").
|
||||||
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
|
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
|
||||||
NewStep("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}),
|
NewStep("TestCase3").CallRefCase(&TestCase{Config: NewConfig("TestCase3")}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
testcase2 := &TestCasePath{demoTestCaseJSONPath}
|
testcase2 := &TestCasePath{demoTestCaseJSONPath}
|
||||||
|
|||||||
@@ -8,17 +8,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var demoTestCase = &hrp.TestCase{
|
var demoTestCase = &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("demo with complex mechanisms").
|
||||||
Name: "demo with complex mechanisms",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
WithVariables(map[string]interface{}{ // global level variables
|
||||||
Variables: map[string]interface{}{ // global level variables
|
|
||||||
"n": 5,
|
"n": 5,
|
||||||
"a": 12.3,
|
"a": 12.3,
|
||||||
"b": 3.45,
|
"b": 3.45,
|
||||||
"varFoo1": "${gen_random_string($n)}",
|
"varFoo1": "${gen_random_string($n)}",
|
||||||
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
|
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
|
||||||
},
|
}),
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{ // step level variables
|
WithVariables(map[string]interface{}{ // step level variables
|
||||||
|
|||||||
@@ -9,11 +9,9 @@ import (
|
|||||||
// reference extracted variables for validation in the same step
|
// reference extracted variables for validation in the same step
|
||||||
func TestCaseExtractStepSingle(t *testing.T) {
|
func TestCaseExtractStepSingle(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with variables").
|
||||||
Name: "run request with variables",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
@@ -46,11 +44,9 @@ func TestCaseExtractStepSingle(t *testing.T) {
|
|||||||
// reference extracted variables from previous step
|
// reference extracted variables from previous step
|
||||||
func TestCaseExtractStepAssociation(t *testing.T) {
|
func TestCaseExtractStepAssociation(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with variables").
|
||||||
Name: "run request with variables",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
|
|||||||
@@ -8,16 +8,14 @@ import (
|
|||||||
|
|
||||||
func TestCaseCallFunction(t *testing.T) {
|
func TestCaseCallFunction(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with functions").
|
||||||
Name: "run request with functions",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
WithVariables(map[string]interface{}{
|
||||||
Verify: false,
|
|
||||||
Variables: map[string]interface{}{
|
|
||||||
"n": 5,
|
"n": 5,
|
||||||
"a": 12.3,
|
"a": 12.3,
|
||||||
"b": 3.45,
|
"b": 3.45,
|
||||||
},
|
}).
|
||||||
},
|
SetVerifySSL(false),
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
GET("/get").
|
GET("/get").
|
||||||
|
|||||||
@@ -8,11 +8,9 @@ import (
|
|||||||
|
|
||||||
func TestCaseBasicRequest(t *testing.T) {
|
func TestCaseBasicRequest(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("request methods testcase in hardcode").
|
||||||
Name: "request methods testcase in hardcode",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
GET("/get").
|
GET("/get").
|
||||||
|
|||||||
@@ -8,11 +8,9 @@ import (
|
|||||||
|
|
||||||
func TestCaseValidateStep(t *testing.T) {
|
func TestCaseValidateStep(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with validation").
|
||||||
Name: "run request with validation",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
|
|||||||
@@ -8,16 +8,13 @@ import (
|
|||||||
|
|
||||||
func TestCaseConfigVariables(t *testing.T) {
|
func TestCaseConfigVariables(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with variables").
|
||||||
Name: "run request with variables",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
WithVariables(map[string]interface{}{
|
||||||
Variables: map[string]interface{}{
|
|
||||||
"var1": "bar1",
|
"var1": "bar1",
|
||||||
"agent": "HttpRunnerPlus",
|
"agent": "HttpRunnerPlus",
|
||||||
"expectedStatusCode": 200,
|
"expectedStatusCode": 200,
|
||||||
},
|
}).SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
GET("/get").
|
GET("/get").
|
||||||
@@ -41,11 +38,9 @@ func TestCaseConfigVariables(t *testing.T) {
|
|||||||
|
|
||||||
func TestCaseStepVariables(t *testing.T) {
|
func TestCaseStepVariables(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with variables").
|
||||||
Name: "run request with variables",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
@@ -74,16 +69,13 @@ func TestCaseStepVariables(t *testing.T) {
|
|||||||
|
|
||||||
func TestCaseOverrideConfigVariables(t *testing.T) {
|
func TestCaseOverrideConfigVariables(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with variables").
|
||||||
Name: "run request with variables",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
WithVariables(map[string]interface{}{
|
||||||
Variables: map[string]interface{}{
|
|
||||||
"var1": "bar0",
|
"var1": "bar0",
|
||||||
"agent": "HttpRunnerPlus",
|
"agent": "HttpRunnerPlus",
|
||||||
"expectedStatusCode": 200,
|
"expectedStatusCode": 200,
|
||||||
},
|
}).SetVerifySSL(false),
|
||||||
Verify: false,
|
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
@@ -112,18 +104,15 @@ func TestCaseOverrideConfigVariables(t *testing.T) {
|
|||||||
|
|
||||||
func TestCaseParseVariables(t *testing.T) {
|
func TestCaseParseVariables(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.TConfig{
|
Config: hrp.NewConfig("run request with functions").
|
||||||
Name: "run request with functions",
|
SetBaseURL("https://postman-echo.com").
|
||||||
BaseURL: "https://postman-echo.com",
|
WithVariables(map[string]interface{}{
|
||||||
Verify: false,
|
|
||||||
Variables: map[string]interface{}{
|
|
||||||
"n": 5,
|
"n": 5,
|
||||||
"a": 12.3,
|
"a": 12.3,
|
||||||
"b": 3.45,
|
"b": 3.45,
|
||||||
"varFoo1": "${gen_random_string($n)}",
|
"varFoo1": "${gen_random_string($n)}",
|
||||||
"varFoo2": "${max($a, $b)}", // 12.3
|
"varFoo2": "${max($a, $b)}", // 12.3
|
||||||
},
|
}).SetVerifySSL(false),
|
||||||
},
|
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func (h *HAR) makeTestCase() (*hrp.TCase, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tCase := &hrp.TCase{
|
tCase := &hrp.TCase{
|
||||||
Config: *h.prepareConfig(),
|
Config: h.prepareConfig(),
|
||||||
TestSteps: teststeps,
|
TestSteps: teststeps,
|
||||||
}
|
}
|
||||||
return tCase, nil
|
return tCase, nil
|
||||||
@@ -114,11 +114,8 @@ func (h *HAR) load() (*Har, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *HAR) prepareConfig() *hrp.TConfig {
|
func (h *HAR) prepareConfig() *hrp.TConfig {
|
||||||
return &hrp.TConfig{
|
return hrp.NewConfig("testcase description").
|
||||||
Name: "testcase description",
|
SetVerifySSL(false)
|
||||||
Variables: make(map[string]interface{}),
|
|
||||||
Verify: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) {
|
func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ type TStep struct {
|
|||||||
// TCase represents testcase data structure.
|
// TCase represents testcase data structure.
|
||||||
// Each testcase includes one public config and several sequential teststeps.
|
// Each testcase includes one public config and several sequential teststeps.
|
||||||
type TCase struct {
|
type TCase struct {
|
||||||
Config TConfig `json:"config" yaml:"config"`
|
Config *TConfig `json:"config" yaml:"config"`
|
||||||
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
|
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ type ITestCase interface {
|
|||||||
// TestCase is a container for one testcase.
|
// TestCase is a container for one testcase.
|
||||||
// used for testcase runner
|
// used for testcase runner
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
Config TConfig
|
Config *TConfig
|
||||||
TestSteps []IStep
|
TestSteps []IStep
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func (r *runner) Run(testcases ...ITestCase) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *runner) runCase(testcase *TestCase) error {
|
func (r *runner) runCase(testcase *TestCase) error {
|
||||||
config := &testcase.Config
|
config := testcase.Config
|
||||||
if err := r.parseConfig(config); err != nil {
|
if err := r.parseConfig(config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ import (
|
|||||||
|
|
||||||
func TestHttpRunner(t *testing.T) {
|
func TestHttpRunner(t *testing.T) {
|
||||||
testcase1 := &TestCase{
|
testcase1 := &TestCase{
|
||||||
Config: TConfig{
|
Config: NewConfig("TestCase1").
|
||||||
Name: "TestCase1",
|
SetBaseURL("http://httpbin.org"),
|
||||||
BaseURL: "http://httpbin.org",
|
|
||||||
},
|
|
||||||
TestSteps: []IStep{
|
TestSteps: []IStep{
|
||||||
NewStep("headers").
|
NewStep("headers").
|
||||||
GET("/headers").
|
GET("/headers").
|
||||||
@@ -21,14 +19,11 @@ func TestHttpRunner(t *testing.T) {
|
|||||||
Validate().
|
Validate().
|
||||||
AssertEqual("status_code", 200, "check status code").
|
AssertEqual("status_code", 200, "check status code").
|
||||||
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
|
AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"),
|
||||||
NewStep("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}),
|
NewStep("TestCase3").CallRefCase(&TestCase{Config: NewConfig("TestCase3")}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
testcase2 := &TestCase{
|
testcase2 := &TestCase{
|
||||||
Config: TConfig{
|
Config: NewConfig("TestCase2").SetWeight(3),
|
||||||
Name: "TestCase2",
|
|
||||||
Weight: 3,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
testcase3 := &TestCasePath{demoTestCaseJSONPath}
|
testcase3 := &TestCasePath{demoTestCaseJSONPath}
|
||||||
|
|
||||||
|
|||||||
38
step.go
38
step.go
@@ -2,6 +2,44 @@ package hrp
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
|
// NewConfig returns a new constructed testcase config with specified testcase name.
|
||||||
|
func NewConfig(name string) *TConfig {
|
||||||
|
return &TConfig{
|
||||||
|
Name: name,
|
||||||
|
Variables: make(map[string]interface{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TConfig) WithVariables(variables map[string]interface{}) *TConfig {
|
||||||
|
c.Variables = variables
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TConfig) SetBaseURL(baseURL string) *TConfig {
|
||||||
|
c.BaseURL = baseURL
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TConfig) SetVerifySSL(verify bool) *TConfig {
|
||||||
|
c.Verify = verify
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TConfig) WithParameters(parameters map[string]interface{}) *TConfig {
|
||||||
|
c.Parameters = parameters
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TConfig) ExportVars(vars ...string) *TConfig {
|
||||||
|
c.Export = vars
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TConfig) SetWeight(weight int) *TConfig {
|
||||||
|
c.Weight = weight
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// NewStep returns a new constructed teststep with specified step name.
|
// NewStep returns a new constructed teststep with specified step name.
|
||||||
func NewStep(name string) *step {
|
func NewStep(name string) *step {
|
||||||
return &step{
|
return &step{
|
||||||
|
|||||||
@@ -74,9 +74,7 @@ func TestRunRequestPostDataToStruct(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunRequestRun(t *testing.T) {
|
func TestRunRequestRun(t *testing.T) {
|
||||||
config := &TConfig{
|
config := NewConfig("test").SetBaseURL("https://postman-echo.com")
|
||||||
BaseURL: "https://postman-echo.com",
|
|
||||||
}
|
|
||||||
runner := NewRunner(t).SetDebug(true)
|
runner := NewRunner(t).SetDebug(true)
|
||||||
if _, err := runner.runStep(stepGET, config); err != nil {
|
if _, err := runner.runStep(stepGET, config); err != nil {
|
||||||
t.Fatalf("tStep.Run() error: %s", err)
|
t.Fatalf("tStep.Run() error: %s", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user