mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-08 17:09:33 +08:00
feat: load test cases from TestCaseJSON
This commit is contained in:
@@ -351,7 +351,7 @@ func sha256HMAC(key []byte, data []byte) []byte {
|
|||||||
return []byte(fmt.Sprintf("%x", mac.Sum(nil)))
|
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 {
|
func Sign(ver string, ak string, sk string, body []byte) string {
|
||||||
expiration := 1800
|
expiration := 1800
|
||||||
signKeyInfo := fmt.Sprintf("%s/%s/%d/%d", ver, ak, time.Now().Unix(), expiration)
|
signKeyInfo := fmt.Sprintf("%s/%s/%d/%d", ver, ak, time.Now().Unix(), expiration)
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v5.0.0+2412262256
|
v5.0.0+2501062030
|
||||||
|
|||||||
@@ -25,6 +25,15 @@ func LoadTestCases(tests ...ITestCase) ([]*TestCase, error) {
|
|||||||
continue
|
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
|
// iTestCase should be a TestCasePath, file path or folder path
|
||||||
tcPath, ok := iTestCase.(*TestCasePath)
|
tcPath, ok := iTestCase.(*TestCasePath)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -48,6 +48,23 @@ func TestLoadTestCases(t *testing.T) {
|
|||||||
if !assert.Equal(t, len(testCases), 1) {
|
if !assert.Equal(t, len(testCases), 1) {
|
||||||
t.Fatal()
|
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) {
|
func TestLoadCase(t *testing.T) {
|
||||||
|
|||||||
+15
-1
@@ -10,10 +10,11 @@ import (
|
|||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/code"
|
"github.com/httprunner/httprunner/v4/hrp/code"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ITestCase represents interface for testcases,
|
// ITestCase represents interface for testcases,
|
||||||
// includes TestCase and TestCasePath.
|
// includes TestCase, TestCasePath and TestCaseJSON
|
||||||
type ITestCase interface {
|
type ITestCase interface {
|
||||||
GetTestCase() (*TestCase, error)
|
GetTestCase() (*TestCase, error)
|
||||||
}
|
}
|
||||||
@@ -41,6 +42,19 @@ func (path *TestCasePath) GetTestCase() (*TestCase, error) {
|
|||||||
return tc.loadISteps()
|
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 is a container for one testcase, which is used for testcase runner.
|
||||||
// TestCase implements ITestCase interface.
|
// TestCase implements ITestCase interface.
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user