feat: load test cases from TestCaseJSON

This commit is contained in:
lilong.129
2025-01-06 20:30:13 +08:00
parent 2d8999148e
commit 8fd9723d4a
5 changed files with 43 additions and 3 deletions

View File

@@ -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)

View File

@@ -1 +1 @@
v5.0.0+2412262256
v5.0.0+2501062030

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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 {