mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 08:59:44 +08:00
refactor: change TStep API/TestCase type
This commit is contained in:
@@ -103,35 +103,51 @@ func (tc *TCase) ToTestCase() (*TestCase, error) {
|
||||
log.Info().Str("dir", projectRootDir).Msg("located project root dir")
|
||||
|
||||
for _, step := range tc.TestSteps {
|
||||
if step.APIPath != "" {
|
||||
path := filepath.Join(projectRootDir, step.APIPath)
|
||||
if !builtin.IsFilePathExists(path) {
|
||||
return nil, errors.New("referenced api file not found: " + path)
|
||||
if step.API != nil {
|
||||
if apiContent, ok := step.API.(*API); ok {
|
||||
step.API = apiContent
|
||||
testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
|
||||
step: step,
|
||||
})
|
||||
return testCase, nil
|
||||
}
|
||||
|
||||
refAPI := APIPath(path)
|
||||
step.APIContent = &refAPI
|
||||
apiContent, err := step.APIContent.ToAPI()
|
||||
// reference api path
|
||||
var apiFullPath string
|
||||
if apiPath, ok := step.API.(string); ok {
|
||||
apiFullPath = filepath.Join(projectRootDir, apiPath)
|
||||
} else if apiPath, ok := step.API.(APIPath); ok {
|
||||
apiFullPath = filepath.Join(projectRootDir, apiPath.GetPath())
|
||||
} else {
|
||||
return nil, errors.New("invalid api format")
|
||||
}
|
||||
|
||||
if !builtin.IsFilePathExists(apiFullPath) {
|
||||
return nil, errors.New("referenced api file not found: " + apiFullPath)
|
||||
}
|
||||
|
||||
refAPI := APIPath(apiFullPath)
|
||||
apiContent, err := refAPI.ToAPI()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
step.APIContent = apiContent
|
||||
step.API = apiContent
|
||||
|
||||
testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
|
||||
step: step,
|
||||
})
|
||||
} else if step.TestCasePath != "" {
|
||||
path := filepath.Join(projectRootDir, step.TestCasePath)
|
||||
} else if step.TestCase != nil {
|
||||
path := filepath.Join(projectRootDir, step.TestCase.(string))
|
||||
if !builtin.IsFilePathExists(path) {
|
||||
return nil, errors.New("referenced testcase file not found: " + path)
|
||||
}
|
||||
|
||||
refTestCase := TestCasePath(path)
|
||||
step.TestCaseContent = &refTestCase
|
||||
tc, err := step.TestCaseContent.ToTestCase()
|
||||
tc, err := refTestCase.ToTestCase()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
step.TestCaseContent = tc
|
||||
step.TestCase = tc
|
||||
testCase.TestSteps = append(testCase.TestSteps, &StepTestCaseWithOptionalArgs{
|
||||
step: step,
|
||||
})
|
||||
|
||||
@@ -222,21 +222,19 @@ type IAPI interface {
|
||||
// TStep represents teststep data structure.
|
||||
// Each step maybe two different type: make one HTTP request or reference another testcase.
|
||||
type TStep struct {
|
||||
Name string `json:"name" yaml:"name"` // required
|
||||
Request *Request `json:"request,omitempty" yaml:"request,omitempty"`
|
||||
APIPath string `json:"api,omitempty" yaml:"api,omitempty"`
|
||||
APIContent IAPI `json:"api_content,omitempty" yaml:"api_content,omitempty"`
|
||||
TestCasePath string `json:"testcase,omitempty" yaml:"testcase,omitempty"`
|
||||
TestCaseContent ITestCase `json:"testcase_content,omitempty" yaml:"testcase_content,omitempty"`
|
||||
Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"`
|
||||
Rendezvous *Rendezvous `json:"rendezvous,omitempty" yaml:"rendezvous,omitempty"`
|
||||
ThinkTime *ThinkTime `json:"think_time,omitempty" yaml:"think_time,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||
SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"`
|
||||
TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"`
|
||||
Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"`
|
||||
Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
|
||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||
Name string `json:"name" yaml:"name"` // required
|
||||
Request *Request `json:"request,omitempty" yaml:"request,omitempty"`
|
||||
API interface{} `json:"api,omitempty" yaml:"api,omitempty"` // *APIPath or *API
|
||||
TestCase interface{} `json:"testcase,omitempty" yaml:"testcase,omitempty"` // *TestCasePath or *TestCase
|
||||
Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"`
|
||||
Rendezvous *Rendezvous `json:"rendezvous,omitempty" yaml:"rendezvous,omitempty"`
|
||||
ThinkTime *ThinkTime `json:"think_time,omitempty" yaml:"think_time,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||
SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"`
|
||||
TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"`
|
||||
Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"`
|
||||
Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
|
||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||
}
|
||||
|
||||
type stepType string
|
||||
|
||||
@@ -345,7 +345,7 @@ func (r *caseRunner) runStep(index int, caseConfig *TConfig) (stepResult *stepDa
|
||||
if _, ok := step.(*StepAPIWithOptionalArgs); ok {
|
||||
// run referenced API
|
||||
log.Info().Str("api", copiedStep.Name).Msg("run referenced api")
|
||||
api, _ := copiedStep.APIContent.ToAPI()
|
||||
api, _ := copiedStep.API.(*API)
|
||||
extendWithAPI(copiedStep, api)
|
||||
}
|
||||
// override headers
|
||||
@@ -967,7 +967,7 @@ func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err err
|
||||
StepType: stepTypeTestCase,
|
||||
Success: false,
|
||||
}
|
||||
testcase := step.TestCaseContent
|
||||
testcase := step.TestCase.(*TestCase)
|
||||
|
||||
// copy testcase to avoid data racing
|
||||
copiedTestCase := &TestCase{}
|
||||
|
||||
24
hrp/step.go
24
hrp/step.go
@@ -169,7 +169,7 @@ func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs {
|
||||
// CallRefCase calls a referenced testcase.
|
||||
func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs {
|
||||
var err error
|
||||
s.step.TestCaseContent, err = tc.ToTestCase()
|
||||
s.step.TestCase, err = tc.ToTestCase()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to load testcase")
|
||||
os.Exit(1)
|
||||
@@ -182,7 +182,7 @@ func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs {
|
||||
// CallRefAPI calls a referenced api.
|
||||
func (s *StepRequest) CallRefAPI(api IAPI) *StepAPIWithOptionalArgs {
|
||||
var err error
|
||||
s.step.APIContent, err = api.ToAPI()
|
||||
s.step.API, err = api.ToAPI()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to load api")
|
||||
os.Exit(1)
|
||||
@@ -332,8 +332,10 @@ func (s *StepAPIWithOptionalArgs) TeardownHook(hook string) *StepAPIWithOptional
|
||||
|
||||
// Export specifies variable names to export from referenced api for current step.
|
||||
func (s *StepAPIWithOptionalArgs) Export(names ...string) *StepAPIWithOptionalArgs {
|
||||
api, _ := s.step.APIContent.ToAPI()
|
||||
s.step.Export = append(api.Export, names...)
|
||||
api, ok := s.step.API.(*API)
|
||||
if ok {
|
||||
s.step.Export = append(api.Export, names...)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -341,8 +343,11 @@ func (s *StepAPIWithOptionalArgs) Name() string {
|
||||
if s.step.Name != "" {
|
||||
return s.step.Name
|
||||
}
|
||||
api, _ := s.step.APIContent.ToAPI()
|
||||
return api.Name
|
||||
api, ok := s.step.API.(*API)
|
||||
if ok {
|
||||
return api.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *StepAPIWithOptionalArgs) Type() string {
|
||||
@@ -374,8 +379,11 @@ func (s *StepTestCaseWithOptionalArgs) Name() string {
|
||||
if s.step.Name != "" {
|
||||
return s.step.Name
|
||||
}
|
||||
ts, _ := s.step.TestCaseContent.ToTestCase()
|
||||
return ts.Config.Name
|
||||
ts, ok := s.step.TestCase.(*TestCase)
|
||||
if ok {
|
||||
return ts.Config.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *StepTestCaseWithOptionalArgs) Type() string {
|
||||
|
||||
Reference in New Issue
Block a user