mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 10:59:42 +08:00
refactor: convert API
This commit is contained in:
@@ -35,12 +35,12 @@ func (b *Boomer) SetDebug(debug bool) *Boomer {
|
||||
|
||||
func (b *Boomer) Run(testcases ...ITestCase) {
|
||||
var taskSlice []*boomer.Task
|
||||
for _, testcase := range testcases {
|
||||
tcStruct, err := testcase.ToStruct()
|
||||
for _, iTestCase := range testcases {
|
||||
testcase, err := iTestCase.ToTestCase()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
task := b.convertBoomerTask(tcStruct)
|
||||
task := b.convertBoomerTask(testcase)
|
||||
taskSlice = append(taskSlice, task)
|
||||
}
|
||||
boomer.Run(taskSlice...)
|
||||
|
||||
32
convert.go
32
convert.go
@@ -11,25 +11,24 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func (tc *TestCase) toStruct() *TCase {
|
||||
tcStruct := TCase{
|
||||
func (tc *TestCase) ToTCase() (*TCase, error) {
|
||||
tCase := TCase{
|
||||
Config: tc.Config,
|
||||
}
|
||||
for _, step := range tc.TestSteps {
|
||||
tcStruct.TestSteps = append(tcStruct.TestSteps, step.ToStruct())
|
||||
tCase.TestSteps = append(tCase.TestSteps, step.ToStruct())
|
||||
}
|
||||
return &tcStruct
|
||||
return &tCase, nil
|
||||
}
|
||||
|
||||
func (tc *TestCase) dump2JSON(path string) error {
|
||||
func (tc *TCase) Dump2JSON(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 json path: %s", path)
|
||||
tcStruct := tc.toStruct()
|
||||
file, _ := json.MarshalIndent(tcStruct, "", " ")
|
||||
file, _ := json.MarshalIndent(tc, "", " ")
|
||||
err = ioutil.WriteFile(path, file, 0644)
|
||||
if err != nil {
|
||||
log.Printf("dump json path error: %v", err)
|
||||
@@ -38,7 +37,7 @@ func (tc *TestCase) dump2JSON(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tc *TestCase) dump2YAML(path string) error {
|
||||
func (tc *TCase) Dump2YAML(path string) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Printf("convert absolute path error: %v, path: %v", err, path)
|
||||
@@ -52,8 +51,7 @@ func (tc *TestCase) dump2YAML(path string) error {
|
||||
encoder.SetIndent(4)
|
||||
|
||||
// encode
|
||||
tcStruct := tc.toStruct()
|
||||
err = encoder.Encode(tcStruct)
|
||||
err = encoder.Encode(tc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -106,7 +104,7 @@ func loadFromYAML(path string) (*TCase, error) {
|
||||
return tc, err
|
||||
}
|
||||
|
||||
func convertTestCase(tc *TCase) (*TestCase, error) {
|
||||
func (tc *TCase) ToTestCase() (*TestCase, error) {
|
||||
testCase := &TestCase{
|
||||
Config: tc.Config,
|
||||
}
|
||||
@@ -128,7 +126,7 @@ func convertTestCase(tc *TCase) (*TestCase, error) {
|
||||
|
||||
var ErrUnsupportedFileExt = fmt.Errorf("unsupported testcase file extension")
|
||||
|
||||
func loadTestFile(path *TestCasePath) (*TestCase, error) {
|
||||
func (path *TestCasePath) ToTestCase() (*TestCase, error) {
|
||||
var tc *TCase
|
||||
var err error
|
||||
|
||||
@@ -145,9 +143,17 @@ func loadTestFile(path *TestCasePath) (*TestCase, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
testcase, err := convertTestCase(tc)
|
||||
testcase, err := tc.ToTestCase()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return testcase, nil
|
||||
}
|
||||
|
||||
func (path *TestCasePath) ToTCase() (*TCase, error) {
|
||||
testcase, err := path.ToTestCase()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return testcase.ToTCase()
|
||||
}
|
||||
|
||||
@@ -56,12 +56,13 @@ var (
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
tCase, _ := demoTestCase.ToTCase()
|
||||
// setup, prepare demo json/yaml testcase file path
|
||||
err := demoTestCase.dump2JSON(demoTestCaseJSONPath)
|
||||
err := tCase.Dump2JSON(demoTestCaseJSONPath)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
err = demoTestCase.dump2YAML(demoTestCaseYAMLPath)
|
||||
err = tCase.Dump2YAML(demoTestCaseYAMLPath)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -118,7 +119,7 @@ func TestLoadYAMLCase(t *testing.T) {
|
||||
|
||||
func TestLoadJSONAndRun(t *testing.T) {
|
||||
jsonPath := &TestCasePath{demoTestCaseJSONPath}
|
||||
testcase, err := loadTestFile(jsonPath)
|
||||
testcase, err := jsonPath.ToTestCase()
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
13
models.go
13
models.go
@@ -68,7 +68,8 @@ type IStep interface {
|
||||
}
|
||||
|
||||
type ITestCase interface {
|
||||
ToStruct() (*TestCase, error)
|
||||
ToTestCase() (*TestCase, error)
|
||||
ToTCase() (*TCase, error)
|
||||
}
|
||||
|
||||
// used for testcase runner
|
||||
@@ -77,7 +78,7 @@ type TestCase struct {
|
||||
TestSteps []IStep
|
||||
}
|
||||
|
||||
func (tc *TestCase) ToStruct() (*TestCase, error) {
|
||||
func (tc *TestCase) ToTestCase() (*TestCase, error) {
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
@@ -85,14 +86,6 @@ type TestCasePath struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (path *TestCasePath) ToStruct() (*TestCase, error) {
|
||||
testcase, err := loadTestFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return testcase, nil
|
||||
}
|
||||
|
||||
type TestCaseSummary struct{}
|
||||
|
||||
type StepData struct {
|
||||
|
||||
@@ -38,13 +38,13 @@ func (r *Runner) SetDebug(debug bool) *Runner {
|
||||
}
|
||||
|
||||
func (r *Runner) Run(testcases ...ITestCase) error {
|
||||
for _, testcase := range testcases {
|
||||
tcStruct, err := testcase.ToStruct()
|
||||
for _, iTestCase := range testcases {
|
||||
testcase, err := iTestCase.ToTestCase()
|
||||
if err != nil {
|
||||
log.Printf("[Run] testcase.ToStruct() error: %v", err)
|
||||
return err
|
||||
}
|
||||
if err := r.runCase(tcStruct); err != nil {
|
||||
if err := r.runCase(testcase); err != nil {
|
||||
log.Printf("[Run] runCase error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user