From 8fd9723d4a0275d999311011756c238fa5eff2df Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 6 Jan 2025 20:30:13 +0800 Subject: [PATCH] feat: load test cases from TestCaseJSON --- hrp/internal/builtin/utils.go | 2 +- hrp/internal/version/VERSION | 2 +- hrp/loader.go | 9 +++++++++ hrp/loader_test.go | 17 +++++++++++++++++ hrp/testcase.go | 16 +++++++++++++++- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index cde8e349..2fae638d 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -351,7 +351,7 @@ func sha256HMAC(key []byte, data []byte) []byte { return []byte(fmt.Sprintf("%x", mac.Sum(nil))) } -// ver: auth-v1or auth-v2 +// ver: auth-v1 or auth-v2 func Sign(ver string, ak string, sk string, body []byte) string { expiration := 1800 signKeyInfo := fmt.Sprintf("%s/%s/%d/%d", ver, ak, time.Now().Unix(), expiration) diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index e37ddc88..97969b8f 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2412262256 +v5.0.0+2501062030 diff --git a/hrp/loader.go b/hrp/loader.go index 905cba60..db9f10b9 100644 --- a/hrp/loader.go +++ b/hrp/loader.go @@ -25,6 +25,15 @@ func LoadTestCases(tests ...ITestCase) ([]*TestCase, error) { continue } + if testcase, ok := iTestCase.(*TestCaseJSON); ok { + tc, err := testcase.GetTestCase() + if err != nil { + return nil, err + } + testCases = append(testCases, tc) + continue + } + // iTestCase should be a TestCasePath, file path or folder path tcPath, ok := iTestCase.(*TestCasePath) if !ok { diff --git a/hrp/loader_test.go b/hrp/loader_test.go index 9cb29d81..a870217a 100644 --- a/hrp/loader_test.go +++ b/hrp/loader_test.go @@ -48,6 +48,23 @@ func TestLoadTestCases(t *testing.T) { if !assert.Equal(t, len(testCases), 1) { t.Fatal() } + + // load test cases from TestCaseJSON + testcaseJSON := TestCaseJSON(` + { + "config":{"name":"TestCaseJSON"}, + "teststeps":[ + {"name": "step1", "request":{"url": "https://httpbin.org/get"}}, + {"name": "step2", "shell":{"string": "ls -l"}} + ] + }`) + testCases, err = LoadTestCases(&testcaseJSON) + if !assert.Nil(t, err) { + t.Fatal() + } + if !assert.Equal(t, len(testCases), 1) { + t.Fatal() + } } func TestLoadCase(t *testing.T) { diff --git a/hrp/testcase.go b/hrp/testcase.go index dc675497..ac8df767 100644 --- a/hrp/testcase.go +++ b/hrp/testcase.go @@ -10,10 +10,11 @@ import ( "github.com/httprunner/httprunner/v4/hrp/code" "github.com/httprunner/httprunner/v4/hrp/internal/builtin" + "github.com/httprunner/httprunner/v4/hrp/internal/json" ) // ITestCase represents interface for testcases, -// includes TestCase and TestCasePath. +// includes TestCase, TestCasePath and TestCaseJSON type ITestCase interface { GetTestCase() (*TestCase, error) } @@ -41,6 +42,19 @@ func (path *TestCasePath) GetTestCase() (*TestCase, error) { return tc.loadISteps() } +// TestCaseJSON implements ITestCase interface. +type TestCaseJSON string + +// GetTestCase unmarshal json string and convert to *TestCase +func (tc *TestCaseJSON) GetTestCase() (*TestCase, error) { + var testCaseDef TestCaseDef + err := json.Unmarshal([]byte(*tc), &testCaseDef) + if err != nil { + return nil, errors.Wrap(err, "unmarshal TestCaseJSON failed") + } + return testCaseDef.loadISteps() +} + // TestCase is a container for one testcase, which is used for testcase runner. // TestCase implements ITestCase interface. type TestCase struct {