refactor: NewConfig

This commit is contained in:
debugtalk
2021-12-07 13:29:06 +08:00
parent f7ab39374c
commit 133a737fb4
14 changed files with 88 additions and 86 deletions

View File

@@ -58,7 +58,7 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
Weight: testcase.Config.Weight,
Fn: func() {
runner := NewRunner(nil).SetDebug(b.debug)
config := &testcase.Config
config := testcase.Config
for _, step := range testcase.TestSteps {
var err error
start := time.Now()

View File

@@ -7,10 +7,7 @@ import (
func TestBoomerStandaloneRun(t *testing.T) {
testcase1 := &TestCase{
Config: TConfig{
Name: "TestCase1",
BaseURL: "http://httpbin.org",
},
Config: NewConfig("TestCase1").SetBaseURL("http://httpbin.org"),
TestSteps: []IStep{
NewStep("headers").
GET("/headers").
@@ -22,7 +19,7 @@ func TestBoomerStandaloneRun(t *testing.T) {
Validate().
AssertEqual("status_code", 200, "check status code").
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}

View File

@@ -8,17 +8,15 @@ import (
)
var demoTestCase = &hrp.TestCase{
Config: hrp.TConfig{
Name: "demo with complex mechanisms",
BaseURL: "https://postman-echo.com",
Variables: map[string]interface{}{ // global level variables
Config: hrp.NewConfig("demo with complex mechanisms").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{ // global level variables
"n": 5,
"a": 12.3,
"b": 3.45,
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
},
},
}),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{ // step level variables

View File

@@ -9,11 +9,9 @@ import (
// reference extracted variables for validation in the same step
func TestCaseExtractStepSingle(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with variables",
BaseURL: "https://postman-echo.com",
Verify: false,
},
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
@@ -46,11 +44,9 @@ func TestCaseExtractStepSingle(t *testing.T) {
// reference extracted variables from previous step
func TestCaseExtractStepAssociation(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with variables",
BaseURL: "https://postman-echo.com",
Verify: false,
},
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{

View File

@@ -8,16 +8,14 @@ import (
func TestCaseCallFunction(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with functions",
BaseURL: "https://postman-echo.com",
Verify: false,
Variables: map[string]interface{}{
Config: hrp.NewConfig("run request with functions").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
},
},
}).
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
GET("/get").

View File

@@ -8,11 +8,9 @@ import (
func TestCaseBasicRequest(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "request methods testcase in hardcode",
BaseURL: "https://postman-echo.com",
Verify: false,
},
Config: hrp.NewConfig("request methods testcase in hardcode").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
GET("/get").

View File

@@ -8,11 +8,9 @@ import (
func TestCaseValidateStep(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with validation",
BaseURL: "https://postman-echo.com",
Verify: false,
},
Config: hrp.NewConfig("run request with validation").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{

View File

@@ -8,16 +8,13 @@ import (
func TestCaseConfigVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with variables",
BaseURL: "https://postman-echo.com",
Variables: map[string]interface{}{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
"expectedStatusCode": 200,
},
Verify: false,
},
}).SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
GET("/get").
@@ -41,11 +38,9 @@ func TestCaseConfigVariables(t *testing.T) {
func TestCaseStepVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with variables",
BaseURL: "https://postman-echo.com",
Verify: false,
},
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
@@ -74,16 +69,13 @@ func TestCaseStepVariables(t *testing.T) {
func TestCaseOverrideConfigVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with variables",
BaseURL: "https://postman-echo.com",
Variables: map[string]interface{}{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"var1": "bar0",
"agent": "HttpRunnerPlus",
"expectedStatusCode": 200,
},
Verify: false,
},
}).SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
@@ -112,18 +104,15 @@ func TestCaseOverrideConfigVariables(t *testing.T) {
func TestCaseParseVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "run request with functions",
BaseURL: "https://postman-echo.com",
Verify: false,
Variables: map[string]interface{}{
Config: hrp.NewConfig("run request with functions").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3
},
},
}).SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{

View File

@@ -86,7 +86,7 @@ func (h *HAR) makeTestCase() (*hrp.TCase, error) {
}
tCase := &hrp.TCase{
Config: *h.prepareConfig(),
Config: h.prepareConfig(),
TestSteps: teststeps,
}
return tCase, nil
@@ -114,11 +114,8 @@ func (h *HAR) load() (*Har, error) {
}
func (h *HAR) prepareConfig() *hrp.TConfig {
return &hrp.TConfig{
Name: "testcase description",
Variables: make(map[string]interface{}),
Verify: false,
}
return hrp.NewConfig("testcase description").
SetVerifySSL(false)
}
func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) {

View File

@@ -56,7 +56,7 @@ type TStep struct {
// TCase represents testcase data structure.
// Each testcase includes one public config and several sequential teststeps.
type TCase struct {
Config TConfig `json:"config" yaml:"config"`
Config *TConfig `json:"config" yaml:"config"`
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
}
@@ -76,7 +76,7 @@ type ITestCase interface {
// TestCase is a container for one testcase.
// used for testcase runner
type TestCase struct {
Config TConfig
Config *TConfig
TestSteps []IStep
}

View File

@@ -99,7 +99,7 @@ func (r *runner) Run(testcases ...ITestCase) error {
}
func (r *runner) runCase(testcase *TestCase) error {
config := &testcase.Config
config := testcase.Config
if err := r.parseConfig(config); err != nil {
return err
}

View File

@@ -6,10 +6,8 @@ import (
func TestHttpRunner(t *testing.T) {
testcase1 := &TestCase{
Config: TConfig{
Name: "TestCase1",
BaseURL: "http://httpbin.org",
},
Config: NewConfig("TestCase1").
SetBaseURL("http://httpbin.org"),
TestSteps: []IStep{
NewStep("headers").
GET("/headers").
@@ -21,14 +19,11 @@ func TestHttpRunner(t *testing.T) {
Validate().
AssertEqual("status_code", 200, "check status code").
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{
Config: TConfig{
Name: "TestCase2",
Weight: 3,
},
Config: NewConfig("TestCase2").SetWeight(3),
}
testcase3 := &TestCasePath{demoTestCaseJSONPath}

38
step.go
View File

@@ -2,6 +2,44 @@ package hrp
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.
func NewStep(name string) *step {
return &step{

View File

@@ -74,9 +74,7 @@ func TestRunRequestPostDataToStruct(t *testing.T) {
}
func TestRunRequestRun(t *testing.T) {
config := &TConfig{
BaseURL: "https://postman-echo.com",
}
config := NewConfig("test").SetBaseURL("https://postman-echo.com")
runner := NewRunner(t).SetDebug(true)
if _, err := runner.runStep(stepGET, config); err != nil {
t.Fatalf("tStep.Run() error: %s", err)