feat: support run testcases in specified folder path #1198

This commit is contained in:
debugtalk
2022-03-28 18:12:51 +08:00
parent 205657588e
commit 1e91b8cb0f
7 changed files with 121 additions and 17 deletions

View File

@@ -10,6 +10,7 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
@@ -148,15 +149,17 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
defer sdk.SendEvent(event.StartTiming("execution"))
// record execution data to summary
s := newOutSummary()
for _, iTestCase := range testcases {
testcase, err := iTestCase.ToTestCase()
if err != nil {
log.Error().Err(err).Msg("[Run] convert ITestCase interface to TestCase struct failed")
return err
}
// load all testcases
testCases, err := loadTestCases(testcases...)
if err != nil {
return err
}
for _, testcase := range testCases {
cfg := testcase.Config
// parse config parameters
err = initParameterIterator(cfg, "runner")
err := initParameterIterator(cfg, "runner")
if err != nil {
log.Error().Interface("parameters", cfg.Parameters).Err(err).Msg("parse config parameters failed")
return err
@@ -201,6 +204,56 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
return nil
}
func loadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
testCases := make([]*TestCase, 0)
for _, iTestCase := range iTestCases {
if _, ok := iTestCase.(*TestCase); ok {
testcase, err := iTestCase.ToTestCase()
if err != nil {
log.Error().Err(err).Msg("failed to convert ITestCase interface to TestCase struct")
return nil, err
}
testCases = append(testCases, testcase)
continue
}
// iTestCase should be a TestCasePath, file path or folder path
testCasePath, ok := iTestCase.(*TestCasePath)
if !ok {
return nil, errors.New("invalid iTestCase type")
}
casePaths := make([]*TestCasePath, 0)
casePath := iTestCase.GetPath()
if builtin.IsFolderPathExists(casePath) {
// folder path
files, err := ioutil.ReadDir(casePath)
if err != nil {
return nil, errors.Wrap(err, "failed to read dir")
}
for _, f := range files {
path := TestCasePath(filepath.Join(casePath, f.Name()))
casePaths = append(casePaths, &path)
}
} else {
// file path
casePaths = append(casePaths, testCasePath)
}
for _, path := range casePaths {
tc, err := path.ToTestCase()
if err != nil {
continue
}
testCases = append(testCases, tc)
}
}
log.Info().Int("count", len(testCases)).Msg("load testcases successfully")
return testCases, nil
}
func (r *HRPRunner) newCaseRunner(testcase *TestCase) *caseRunner {
caseRunner := &caseRunner{
TestCase: testcase,