mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
feat: dump2YAML
This commit is contained in:
+31
@@ -1,10 +1,13 @@
|
|||||||
package httpboomer
|
package httpboomer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (tc *TestCase) toStruct() *TCase {
|
func (tc *TestCase) toStruct() *TCase {
|
||||||
@@ -33,3 +36,31 @@ func (tc *TestCase) dump2JSON(path string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tc *TestCase) dump2YAML(path string) error {
|
||||||
|
path, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("convert absolute path error: %v, path: %v", err, path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Printf("dump testcase to yaml path: %s", path)
|
||||||
|
|
||||||
|
// init yaml encoder
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
encoder := yaml.NewEncoder(buffer)
|
||||||
|
encoder.SetIndent(4)
|
||||||
|
|
||||||
|
// encode
|
||||||
|
tcStruct := tc.toStruct()
|
||||||
|
err = encoder.Encode(tcStruct)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(path, buffer.Bytes(), 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("dump yaml path error: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
+11
-3
@@ -6,8 +6,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDump2JSON(t *testing.T) {
|
var demoTestCase = &TestCase{
|
||||||
testcase := &TestCase{
|
|
||||||
Config: TConfig{
|
Config: TConfig{
|
||||||
Name: "demo with complex mechanisms",
|
Name: "demo with complex mechanisms",
|
||||||
BaseURL: "https://postman-echo.com",
|
BaseURL: "https://postman-echo.com",
|
||||||
@@ -49,7 +48,16 @@ func TestDump2JSON(t *testing.T) {
|
|||||||
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err := testcase.dump2JSON("test.json")
|
|
||||||
|
func TestDump2JSON(t *testing.T) {
|
||||||
|
err := demoTestCase.dump2JSON("demo.json")
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDump2YAML(t *testing.T) {
|
||||||
|
err := demoTestCase.dump2YAML("demo.yml")
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,4 +20,5 @@ require (
|
|||||||
github.com/zeromq/gomq v0.0.0-20201031135124-cef4e507bb8e // indirect
|
github.com/zeromq/gomq v0.0.0-20201031135124-cef4e507bb8e // indirect
|
||||||
github.com/zeromq/gomq/zmtp v0.0.0-20201031135124-cef4e507bb8e // indirect
|
github.com/zeromq/gomq/zmtp v0.0.0-20201031135124-cef4e507bb8e // indirect
|
||||||
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect
|
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,51 +13,51 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TConfig struct {
|
type TConfig struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name" yaml:"name"`
|
||||||
Verify bool `json:"verify,omitempty"`
|
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||||
BaseURL string `json:"base_url,omitempty"`
|
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||||
Variables map[string]interface{} `json:"variables,omitempty"`
|
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||||
Parameters map[string]interface{} `json:"parameters,omitempty"`
|
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||||
Export []string `json:"export,omitempty"`
|
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||||
Weight int `json:"weight,omitempty"`
|
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TRequest struct {
|
type TRequest struct {
|
||||||
Method enumHTTPMethod `json:"method"`
|
Method enumHTTPMethod `json:"method" yaml:"method"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url" yaml:"url"`
|
||||||
Params map[string]interface{} `json:"params,omitempty"`
|
Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
|
||||||
Headers map[string]string `json:"headers,omitempty"`
|
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
|
||||||
Cookies map[string]string `json:"cookies,omitempty"`
|
Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"`
|
||||||
Data interface{} `json:"data,omitempty"`
|
Data interface{} `json:"data,omitempty" yaml:"data,omitempty"`
|
||||||
JSON interface{} `json:"json,omitempty"`
|
JSON interface{} `json:"json,omitempty" yaml:"json,omitempty"`
|
||||||
Timeout float32 `json:"timeout,omitempty"`
|
Timeout float32 `json:"timeout,omitempty" yaml:"timeout,omitempty"`
|
||||||
AllowRedirects bool `json:"allow_redirects,omitempty"`
|
AllowRedirects bool `json:"allow_redirects,omitempty" yaml:"allow_redirects,omitempty"`
|
||||||
Verify bool `json:"verify,omitempty"`
|
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TValidator struct {
|
type TValidator struct {
|
||||||
Check string `json:"check,omitempty"` // get value with jmespath
|
Check string `json:"check,omitempty" yaml:"check,omitempty"` // get value with jmespath
|
||||||
Assert string `json:"assert,omitempty"`
|
Assert string `json:"assert,omitempty" yaml:"assert,omitempty"`
|
||||||
Expect interface{} `json:"expect,omitempty"`
|
Expect interface{} `json:"expect,omitempty" yaml:"expect,omitempty"`
|
||||||
Message string `json:"msg,omitempty"`
|
Message string `json:"msg,omitempty" yaml:"msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TStep struct {
|
type TStep struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name" yaml:"name"`
|
||||||
Request *TRequest `json:"request,omitempty"`
|
Request *TRequest `json:"request,omitempty" yaml:"request,omitempty"`
|
||||||
TestCase *TestCase `json:"testcase,omitempty"`
|
TestCase *TestCase `json:"testcase,omitempty" yaml:"testcase,omitempty"`
|
||||||
Variables map[string]interface{} `json:"variables,omitempty"`
|
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||||
SetupHooks []string `json:"setup_hooks,omitempty"`
|
SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"`
|
||||||
TeardownHooks []string `json:"teardown_hooks,omitempty"`
|
TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"`
|
||||||
Extract map[string]string `json:"extract,omitempty"`
|
Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"`
|
||||||
Validators []TValidator `json:"validate,omitempty"`
|
Validators []TValidator `json:"validate,omitempty" yaml:"validate,omitempty"`
|
||||||
Export []string `json:"export,omitempty"`
|
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// used for testcase json loading and dumping
|
// used for testcase json loading and dumping
|
||||||
type TCase struct {
|
type TCase struct {
|
||||||
Config TConfig `json:"config"`
|
Config TConfig `json:"config" yaml:"config"`
|
||||||
TestSteps []*TStep `json:"teststeps"`
|
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// interface for all types of steps
|
// interface for all types of steps
|
||||||
|
|||||||
Reference in New Issue
Block a user