mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-08 07:51:25 +08:00
refactor: LoadTestCases
This commit is contained in:
@@ -14,12 +14,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
|
|||||||
testCases := make([]*TestCase, 0)
|
testCases := make([]*TestCase, 0)
|
||||||
|
|
||||||
for _, iTestCase := range iTestCases {
|
for _, iTestCase := range iTestCases {
|
||||||
if _, ok := iTestCase.(*TestCase); ok {
|
if testcase, 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)
|
testCases = append(testCases, testcase)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -53,6 +48,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
|
|||||||
testCasePath := TestCasePath(path)
|
testCasePath := TestCasePath(path)
|
||||||
tc, err := testCasePath.ToTestCase()
|
tc, err := testCasePath.ToTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Warn().Err(err).Str("path", path).Msg("load testcase failed")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
testCases = append(testCases, tc)
|
testCases = append(testCases, tc)
|
||||||
|
|||||||
51
hrp/loader_test.go
Normal file
51
hrp/loader_test.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package hrp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadTestCases(t *testing.T) {
|
||||||
|
// load test cases from folder path
|
||||||
|
tc := TestCasePath("../examples/demo-with-py-plugin/testcases/")
|
||||||
|
testCases, err := LoadTestCases(&tc)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, 4, len(testCases)) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load test cases from folder path, including sub folders
|
||||||
|
tc = TestCasePath("../examples/demo-with-py-plugin/")
|
||||||
|
testCases, err = LoadTestCases(&tc)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, 4, len(testCases)) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load test cases from single file path
|
||||||
|
tc = TestCasePath(demoTestCaseWithPluginJSONPath)
|
||||||
|
testCases, err = LoadTestCases(&tc)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, 1, len(testCases)) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load test cases from TestCase instance
|
||||||
|
testcase := &TestCase{
|
||||||
|
Config: NewConfig("TestCase").SetWeight(3),
|
||||||
|
}
|
||||||
|
testCases, err = LoadTestCases(testcase)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, len(testCases), 1) {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -175,7 +175,7 @@ func GetProjectRootDirPath(path string) (rootDir string, err error) {
|
|||||||
rootDir = filepath.Dir(pluginPath)
|
rootDir = filepath.Dir(pluginPath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// fix: no debugtalk file in project but having proj.json created by startpeoject
|
// fix: no debugtalk file in project but having proj.json created by startproject
|
||||||
projPath, err := locateFile(path, projectInfoFile)
|
projPath, err := locateFile(path, projectInfoFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
rootDir = filepath.Dir(projPath)
|
rootDir = filepath.Dir(projPath)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||||
)
|
)
|
||||||
@@ -223,47 +222,3 @@ func TestRunCaseWithRefAPI(t *testing.T) {
|
|||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadTestCases(t *testing.T) {
|
|
||||||
// load test cases from folder path
|
|
||||||
tc := TestCasePath("../examples/demo-with-py-plugin/testcases/")
|
|
||||||
testCases, err := LoadTestCases(&tc)
|
|
||||||
if !assert.Nil(t, err) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
if !assert.Equal(t, 4, len(testCases)) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
|
|
||||||
// load test cases from folder path, including sub folders
|
|
||||||
tc = TestCasePath("../examples/demo-with-py-plugin/")
|
|
||||||
testCases, err = LoadTestCases(&tc)
|
|
||||||
if !assert.Nil(t, err) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
if !assert.Equal(t, 4, len(testCases)) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
|
|
||||||
// load test cases from single file path
|
|
||||||
tc = TestCasePath(demoTestCaseWithPluginJSONPath)
|
|
||||||
testCases, err = LoadTestCases(&tc)
|
|
||||||
if !assert.Nil(t, err) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
if !assert.Equal(t, 1, len(testCases)) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
|
|
||||||
// load test cases from TestCase instance
|
|
||||||
testcase := &TestCase{
|
|
||||||
Config: NewConfig("TestCase").SetWeight(3),
|
|
||||||
}
|
|
||||||
testCases, err = LoadTestCases(testcase)
|
|
||||||
if !assert.Nil(t, err) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
if !assert.Equal(t, len(testCases), 1) {
|
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user