From 14211b7c305f356a3591e7eb60bdecda9cb47ab3 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 19 Aug 2024 21:16:13 +0800 Subject: [PATCH] refactor: LoadTestCases --- hrp/loader.go | 8 ++------ hrp/loader_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ hrp/plugin.go | 2 +- hrp/runner_test.go | 45 ---------------------------------------- 4 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 hrp/loader_test.go diff --git a/hrp/loader.go b/hrp/loader.go index ff737c6d..d8bb8c03 100644 --- a/hrp/loader.go +++ b/hrp/loader.go @@ -14,12 +14,7 @@ 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 - } + if testcase, ok := iTestCase.(*TestCase); ok { testCases = append(testCases, testcase) continue } @@ -53,6 +48,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) { testCasePath := TestCasePath(path) tc, err := testCasePath.ToTestCase() if err != nil { + log.Warn().Err(err).Str("path", path).Msg("load testcase failed") return nil } testCases = append(testCases, tc) diff --git a/hrp/loader_test.go b/hrp/loader_test.go new file mode 100644 index 00000000..a419a984 --- /dev/null +++ b/hrp/loader_test.go @@ -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() + } +} diff --git a/hrp/plugin.go b/hrp/plugin.go index ea9afad9..146c588c 100644 --- a/hrp/plugin.go +++ b/hrp/plugin.go @@ -175,7 +175,7 @@ func GetProjectRootDirPath(path string) (rootDir string, err error) { rootDir = filepath.Dir(pluginPath) 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) if err == nil { rootDir = filepath.Dir(projPath) diff --git a/hrp/runner_test.go b/hrp/runner_test.go index b66c7748..3213c13d 100644 --- a/hrp/runner_test.go +++ b/hrp/runner_test.go @@ -7,7 +7,6 @@ import ( "time" "github.com/rs/zerolog/log" - "github.com/stretchr/testify/assert" "github.com/httprunner/httprunner/v4/hrp/internal/code" ) @@ -223,47 +222,3 @@ func TestRunCaseWithRefAPI(t *testing.T) { 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() - } -}