mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 12:33:59 +08:00
feat: support run testcases in specified folder path #1198
This commit is contained in:
4
.github/workflows/hrp-scaffold.yml
vendored
4
.github/workflows/hrp-scaffold.yml
vendored
@@ -26,7 +26,7 @@ jobs:
|
|||||||
- name: Run start project
|
- name: Run start project
|
||||||
run: ./output/hrp startproject demo
|
run: ./output/hrp startproject demo
|
||||||
- name: Run generated demo tests
|
- name: Run generated demo tests
|
||||||
run: ./output/hrp run demo/testcases/demo_with_funplugin.json demo/testcases/demo_requests.yml demo/testcases/demo_ref_testcase.yml
|
run: ./output/hrp run demo/testcases/
|
||||||
- name: Run demo in examples
|
- name: Run demo in examples
|
||||||
run: |
|
run: |
|
||||||
./output/hrp run examples/demo-with-py-plugin/testcases/demo_with_funplugin.json
|
./output/hrp run examples/demo-with-py-plugin/testcases/demo_with_funplugin.json
|
||||||
@@ -51,7 +51,7 @@ jobs:
|
|||||||
- name: Run start project
|
- name: Run start project
|
||||||
run: ./output/hrp startproject demo --go
|
run: ./output/hrp startproject demo --go
|
||||||
- name: Run generated demo tests
|
- name: Run generated demo tests
|
||||||
run: ./output/hrp run demo/testcases/demo_with_funplugin.json demo/testcases/demo_requests.yml demo/testcases/demo_ref_testcase.yml
|
run: ./output/hrp run demo/testcases/
|
||||||
- name: Run demo in examples
|
- name: Run demo in examples
|
||||||
run: |
|
run: |
|
||||||
go build -o examples/demo-with-go-plugin/debugtalk.bin examples/demo-with-go-plugin/plugin/debugtalk.go
|
go build -o examples/demo-with-go-plugin/debugtalk.bin examples/demo-with-go-plugin/plugin/debugtalk.go
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
**go version**
|
**go version**
|
||||||
|
|
||||||
- feat: add `--profile` flag for har2case to support overwrite headers/cookies with specified yaml/json profile file
|
- feat: add `--profile` flag for har2case to support overwrite headers/cookies with specified yaml/json profile file
|
||||||
|
- feat: support run testcases in specified folder path
|
||||||
- change: integrate [sentry sdk][sentry sdk] for panic reporting and analysis
|
- change: integrate [sentry sdk][sentry sdk] for panic reporting and analysis
|
||||||
- change: lock funplugin version when creating scaffold project
|
- change: lock funplugin version when creating scaffold project
|
||||||
- fix: call referenced api/testcase with relative path
|
- fix: call referenced api/testcase with relative path
|
||||||
@@ -30,8 +31,8 @@
|
|||||||
|
|
||||||
## hrp-v0.7.0 (2022-03-15)
|
## hrp-v0.7.0 (2022-03-15)
|
||||||
|
|
||||||
- feat: support API layer for testcase #94
|
- feat: support API layer for testcase
|
||||||
- feat: support global headers for testcase #95
|
- feat: support global headers for testcase
|
||||||
- feat: support call referenced testcase by path in YAML/JSON testcases
|
- feat: support call referenced testcase by path in YAML/JSON testcases
|
||||||
- fix: decode failure when content-encoding is deflate
|
- fix: decode failure when content-encoding is deflate
|
||||||
- fix: unstable RPS when load testing in high concurrency
|
- fix: unstable RPS when load testing in high concurrency
|
||||||
|
|||||||
@@ -38,11 +38,14 @@ func (b *HRPBoomer) Run(testcases ...ITestCase) {
|
|||||||
defer sdk.SendEvent(event.StartTiming("execution"))
|
defer sdk.SendEvent(event.StartTiming("execution"))
|
||||||
|
|
||||||
var taskSlice []*boomer.Task
|
var taskSlice []*boomer.Task
|
||||||
for _, iTestCase := range testcases {
|
|
||||||
testcase, err := iTestCase.ToTestCase()
|
// load all testcases
|
||||||
if err != nil {
|
testCases, err := loadTestCases(testcases...)
|
||||||
panic(err)
|
if err != nil {
|
||||||
}
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testcase := range testCases {
|
||||||
cfg := testcase.Config
|
cfg := testcase.Config
|
||||||
err = initParameterIterator(cfg, "boomer")
|
err = initParameterIterator(cfg, "boomer")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func (tc *TCase) ToTestCase() (*TestCase, error) {
|
|||||||
var apiFullPath string
|
var apiFullPath string
|
||||||
if apiPath, ok := step.API.(string); ok {
|
if apiPath, ok := step.API.(string); ok {
|
||||||
apiFullPath = filepath.Join(projectRootDir, apiPath)
|
apiFullPath = filepath.Join(projectRootDir, apiPath)
|
||||||
} else if apiPath, ok := step.API.(APIPath); ok {
|
} else if apiPath, ok := step.API.(*APIPath); ok {
|
||||||
apiFullPath = filepath.Join(projectRootDir, apiPath.GetPath())
|
apiFullPath = filepath.Join(projectRootDir, apiPath.GetPath())
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("invalid api format")
|
return nil, errors.New("invalid api format")
|
||||||
|
|||||||
@@ -132,6 +132,18 @@ func IsFilePathExists(path string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsFolderPathExists returns true if path exists and path is folder
|
||||||
|
func IsFolderPathExists(path string) bool {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
// path not exists
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// path exists and is dir
|
||||||
|
return info.IsDir()
|
||||||
|
}
|
||||||
|
|
||||||
func EnsureFolderExists(folderPath string) error {
|
func EnsureFolderExists(folderPath string) error {
|
||||||
if !IsPathExists(folderPath) {
|
if !IsPathExists(folderPath) {
|
||||||
err := CreateFolder(folderPath)
|
err := CreateFolder(folderPath)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
@@ -148,15 +149,17 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
|||||||
defer sdk.SendEvent(event.StartTiming("execution"))
|
defer sdk.SendEvent(event.StartTiming("execution"))
|
||||||
// record execution data to summary
|
// record execution data to summary
|
||||||
s := newOutSummary()
|
s := newOutSummary()
|
||||||
for _, iTestCase := range testcases {
|
|
||||||
testcase, err := iTestCase.ToTestCase()
|
// load all testcases
|
||||||
if err != nil {
|
testCases, err := loadTestCases(testcases...)
|
||||||
log.Error().Err(err).Msg("[Run] convert ITestCase interface to TestCase struct failed")
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, testcase := range testCases {
|
||||||
cfg := testcase.Config
|
cfg := testcase.Config
|
||||||
// parse config parameters
|
// parse config parameters
|
||||||
err = initParameterIterator(cfg, "runner")
|
err := initParameterIterator(cfg, "runner")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Interface("parameters", cfg.Parameters).Err(err).Msg("parse config parameters failed")
|
log.Error().Interface("parameters", cfg.Parameters).Err(err).Msg("parse config parameters failed")
|
||||||
return err
|
return err
|
||||||
@@ -201,6 +204,56 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
|||||||
return nil
|
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 {
|
func (r *HRPRunner) newCaseRunner(testcase *TestCase) *caseRunner {
|
||||||
caseRunner := &caseRunner{
|
caseRunner := &caseRunner{
|
||||||
TestCase: testcase,
|
TestCase: testcase,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/hrp/internal/scaffold"
|
"github.com/httprunner/httprunner/hrp/internal/scaffold"
|
||||||
)
|
)
|
||||||
@@ -283,3 +284,37 @@ func TestRunCaseWithRefAPI(t *testing.T) {
|
|||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadTestCases(t *testing.T) {
|
||||||
|
// load test cases from folder path
|
||||||
|
tc := TestCasePath(templatesDir + "testcases")
|
||||||
|
testCases, err := loadTestCases(&tc)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.GreaterOrEqual(t, len(testCases), 5) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load test cases from single file path
|
||||||
|
tc = demoTestCaseWithPluginJSONPath
|
||||||
|
testCases, err = loadTestCases(&tc)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, len(testCases), 1) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load test cases from TestCase instance
|
||||||
|
testcase := &TestCase{
|
||||||
|
Config: NewConfig("TestCase").SetWeight(3),
|
||||||
|
}
|
||||||
|
testCases, err = loadTestCases(testcase)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, len(testCases), 1) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user