mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
refactor: ITestCase, GetTestCase
This commit is contained in:
+2
-2
@@ -25,7 +25,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
|
|||||||
return nil, errors.New("invalid iTestCase type")
|
return nil, errors.New("invalid iTestCase type")
|
||||||
}
|
}
|
||||||
|
|
||||||
casePath := tcPath.GetPath()
|
casePath := string(*tcPath)
|
||||||
err := fs.WalkDir(os.DirFS(casePath), ".", func(path string, dir fs.DirEntry, e error) error {
|
err := fs.WalkDir(os.DirFS(casePath), ".", func(path string, dir fs.DirEntry, e error) error {
|
||||||
if dir == nil {
|
if dir == nil {
|
||||||
// casePath is a file other than a dir
|
// casePath is a file other than a dir
|
||||||
@@ -46,7 +46,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
|
|||||||
|
|
||||||
// filtered testcases
|
// filtered testcases
|
||||||
testCasePath := TestCasePath(path)
|
testCasePath := TestCasePath(path)
|
||||||
tc, err := testCasePath.ToTestCase()
|
tc, err := testCasePath.GetTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().Err(err).Str("path", path).Msg("load testcase failed")
|
log.Warn().Err(err).Str("path", path).Msg("load testcase failed")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+1
-1
@@ -680,7 +680,7 @@ func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs {
|
|||||||
// CallRefCase calls a referenced testcase.
|
// CallRefCase calls a referenced testcase.
|
||||||
func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs {
|
func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs {
|
||||||
var err error
|
var err error
|
||||||
s.step.TestCase, err = tc.ToTestCase()
|
s.step.TestCase, err = tc.GetTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("failed to load testcase")
|
log.Error().Err(err).Msg("failed to load testcase")
|
||||||
os.Exit(code.GetErrorCode(err))
|
os.Exit(code.GetErrorCode(err))
|
||||||
|
|||||||
+6
-15
@@ -17,21 +17,16 @@ import (
|
|||||||
// ITestCase represents interface for testcases,
|
// ITestCase represents interface for testcases,
|
||||||
// includes TestCase and TestCasePath.
|
// includes TestCase and TestCasePath.
|
||||||
type ITestCase interface {
|
type ITestCase interface {
|
||||||
GetPath() string
|
GetTestCase() (*TestCase, error)
|
||||||
ToTestCase() (*TestCase, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestCasePath implements ITestCase interface.
|
// TestCasePath implements ITestCase interface.
|
||||||
type TestCasePath string
|
type TestCasePath string
|
||||||
|
|
||||||
func (path *TestCasePath) GetPath() string {
|
// GetTestCase loads testcase path and convert to *TestCase
|
||||||
return fmt.Sprintf("%v", *path)
|
func (path *TestCasePath) GetTestCase() (*TestCase, error) {
|
||||||
}
|
|
||||||
|
|
||||||
// ToTestCase loads testcase path and convert to *TestCase
|
|
||||||
func (path *TestCasePath) ToTestCase() (*TestCase, error) {
|
|
||||||
tc := &TestCase{}
|
tc := &TestCase{}
|
||||||
casePath := path.GetPath()
|
casePath := string(*path)
|
||||||
err := builtin.LoadFile(casePath, tc)
|
err := builtin.LoadFile(casePath, tc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -56,11 +51,7 @@ type TestCase struct {
|
|||||||
TestSteps []IStep `json:"-" yaml:"-"`
|
TestSteps []IStep `json:"-" yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *TestCase) GetPath() string {
|
func (tc *TestCase) GetTestCase() (*TestCase, error) {
|
||||||
return tc.Config.Path
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *TestCase) ToTestCase() (*TestCase, error) {
|
|
||||||
return tc, nil
|
return tc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +199,7 @@ func (tc *TestCase) loadISteps() (*TestCase, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
refTestCase := TestCasePath(path)
|
refTestCase := TestCasePath(path)
|
||||||
tc, err := refTestCase.ToTestCase()
|
tc, err := refTestCase.GetTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user