mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
feat: loadFromJSON
This commit is contained in:
19
convert.go
19
convert.go
@@ -64,3 +64,22 @@ func (tc *TestCase) dump2YAML(path string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFromJSON(path string) (*TCase, error) {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Printf("convert absolute path error: %v, path: %v", err, path)
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("load testcase from json path: %s", path)
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Printf("dump json path error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tc := &TCase{}
|
||||
err = json.Unmarshal(file, tc)
|
||||
return tc, err
|
||||
}
|
||||
|
||||
@@ -49,11 +49,28 @@ var demoTestCase = &TestCase{
|
||||
},
|
||||
}
|
||||
|
||||
func TestDump2JSON(t *testing.T) {
|
||||
err := demoTestCase.dump2JSON("demo.json")
|
||||
func TestDumpAndLoadJSON(t *testing.T) {
|
||||
jsonPath := "demo.json"
|
||||
err := demoTestCase.dump2JSON(jsonPath)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
tc, err := loadFromJSON(jsonPath)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
if !assert.Equal(t, tc.Config.Name, demoTestCase.Config.Name) {
|
||||
t.Fail()
|
||||
}
|
||||
if !assert.Equal(t, tc.Config.BaseURL, demoTestCase.Config.BaseURL) {
|
||||
t.Fail()
|
||||
}
|
||||
if !assert.Equal(t, tc.TestSteps[1].Name, demoTestCase.TestSteps[1].Name()) {
|
||||
t.Fail()
|
||||
}
|
||||
if !assert.Equal(t, tc.TestSteps[1].Request, demoTestCase.TestSteps[1].ToStruct().Request) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDump2YAML(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user