change: make API more concise

This commit is contained in:
debugtalk
2021-12-07 21:16:32 +08:00
parent 1a36547ff0
commit bd3aae18f6
15 changed files with 154 additions and 137 deletions

108
README.md
View File

@@ -34,7 +34,7 @@ Since installed, you will get a `hrp` command with multiple sub-commands.
```text ```text
$ hrp -h $ hrp -h
hrp (HttpRunner+) is the next generation for HttpRunner. Enjoy! ✨ 🚀 ✨ hrp (HttpRunner+) is the one-stop solution for HTTP(S) testing. Enjoy! ✨ 🚀 ✨
License: Apache-2.0 License: Apache-2.0
Github: https://github.com/httprunner/hrp Github: https://github.com/httprunner/hrp
@@ -65,17 +65,17 @@ You can use `hrp run` command to run HttpRunner JSON/YAML testcases. The followi
<summary>$ hrp run examples/demo.json</summary> <summary>$ hrp run examples/demo.json</summary>
```text ```text
8:04PM INF Set log to pretty console 9:22PM INF Set log to color console other than JSON format.
8:04PM INF Set log level to INFO 9:22PM INF Set log level to INFO
8:04PM INF [init] SetDebug debug=true 9:22PM INF [init] SetDebug debug=true
8:04PM INF load json testcase path=/Users/debugtalk/MyProjects/HttpRunner-dev/hrp/examples/demo.json 9:22PM INF load json testcase path=/Users/debugtalk/MyProjects/HttpRunner-dev/hrp/examples/demo.json
8:04PM INF call function success arguments=[5] funcName=gen_random_string output=B64R8 9:22PM INF call function success arguments=[5] funcName=gen_random_string output=rWRNY
8:04PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3 9:22PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3
8:04PM INF run testcase start testcase="demo with complex mechanisms" 9:22PM INF run testcase start testcase="demo with complex mechanisms"
8:04PM INF call function success arguments=[12.3,34.5] funcName=max output=34.5 9:22PM INF run step start step="get with params"
8:04PM INF run step start step="get with params" 9:22PM INF call function success arguments=[12.3,34.5] funcName=max output=34.5
-------------------- request -------------------- -------------------- request --------------------
GET /get?foo1=B64R8&foo2=34.5 HTTP/1.1 GET /get?foo1=rWRNY&foo2=34.5 HTTP/1.1
Host: postman-echo.com Host: postman-echo.com
User-Agent: HttpRunnerPlus User-Agent: HttpRunnerPlus
@@ -85,70 +85,70 @@ HTTP/1.1 200 OK
Content-Length: 304 Content-Length: 304
Connection: keep-alive Connection: keep-alive
Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8
Date: Thu, 11 Nov 2021 12:04:32 GMT Date: Tue, 07 Dec 2021 13:22:50 GMT
Etag: W/"130-LUQ0LVU7KVSZha0O3nQxqPlr5dw" Etag: W/"130-gmtE0VWiyE0mXUGoJe5AyhMQ2ig"
Set-Cookie: sails.sid=s%3Ag6vZXrHHzs-B7Q1bFrYQq83dUje_EkSu.06vsqbkZvIOJ6mb1It7c6i354e%2B0t91K4cG14YFjSX0; Path=/; HttpOnly Set-Cookie: sails.sid=s%3AEWPwP8H-nbpSrCseeulwDQ8OEtRy1pGu.aHV6KrEIiFgaJsUAuDmmmJCYiV6XkrHLS%2Fd9g9vtZQw; Path=/; HttpOnly
Vary: Accept-Encoding Vary: Accept-Encoding
{"args":{"foo1":"B64R8","foo2":"34.5"},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-618d06d0-7516144f65e561a8238adab5","user-agent":"HttpRunnerPlus","accept-encoding":"gzip"},"url":"https://postman-echo.com/get?foo1=B64R8&foo2=34.5"} {"args":{"foo1":"rWRNY","foo2":"34.5"},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-61af602a-5eea88ee21122daf4e8dfe95","user-agent":"HttpRunnerPlus","accept-encoding":"gzip"},"url":"https://postman-echo.com/get?foo1=rWRNY&foo2=34.5"}
-------------------------------------------------- --------------------------------------------------
8:04PM INF extract value from=body.args.foo1 value=B64R8 9:22PM INF extract value from=body.args.foo1 value=rWRNY
8:04PM INF set variable value=B64R8 variable=varFoo1 9:22PM INF set variable value=rWRNY variable=varFoo1
8:04PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true 9:22PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true
8:04PM INF validate headers."Content-Type" assertMethod=startswith checkValue="application/json; charset=utf-8" expectValue=application/json result=true 9:22PM INF validate headers."Content-Type" assertMethod=startswith checkValue="application/json; charset=utf-8" expectValue=application/json result=true
8:04PM INF validate body.args.foo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true 9:22PM INF validate body.args.foo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
8:04PM INF validate $varFoo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true 9:22PM INF validate $varFoo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
8:04PM INF validate body.args.foo2 assertMethod=equals checkValue=34.5 expectValue=34.5 result=true 9:22PM INF validate body.args.foo2 assertMethod=equals checkValue=34.5 expectValue=34.5 result=true
8:04PM INF run step end exportVars={"varFoo1":"B64R8"} step="get with params" success=true 9:22PM INF run step end exportVars={"varFoo1":"rWRNY"} step="get with params" success=true
8:04PM INF run step start step="post json data" 9:22PM INF run step start step="post json data"
8:04PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3 9:22PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3
-------------------- request -------------------- -------------------- request --------------------
POST /post HTTP/1.1 POST /post HTTP/1.1
Host: postman-echo.com Host: postman-echo.com
Content-Type: application/json; charset=UTF-8 Content-Type: application/json; charset=UTF-8
{"foo1":"B64R8","foo2":12.3} {"foo1":"rWRNY","foo2":12.3}
==================== response =================== ==================== response ===================
HTTP/1.1 200 OK HTTP/1.1 200 OK
Content-Length: 424 Content-Length: 424
Connection: keep-alive Connection: keep-alive
Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8
Date: Thu, 11 Nov 2021 12:04:32 GMT Date: Tue, 07 Dec 2021 13:22:50 GMT
Etag: W/"1a8-1umvYElau4WkHR7VON+jKXozT2c" Etag: W/"1a8-5fCAlcltnCS4Ed/6OxpH9i9dlKs"
Set-Cookie: sails.sid=s%3AeNnS5IE6TBePzx95OfuwyIweJy5aExb0.7MH6Vb42vbZ6OhNT2nhQGcAmHgqcFmtM8X03Qsoxa1k; Path=/; HttpOnly Set-Cookie: sails.sid=s%3As1b8P7f8sc3JRNumS-XJrzbwb5oxdkOs.pXRRifddVUiWuzAxwBikBxf3ayM8OahgDDzP7kSnMCc; Path=/; HttpOnly
Vary: Accept-Encoding Vary: Accept-Encoding
{"args":{},"data":{"foo1":"B64R8","foo2":12.3},"files":{},"form":{},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-618d06d0-360475ad34903a97191978d7","content-length":"28","user-agent":"Go-http-client/1.1","content-type":"application/json; charset=UTF-8","accept-encoding":"gzip"},"json":{"foo1":"B64R8","foo2":12.3},"url":"https://postman-echo.com/post"} {"args":{},"data":{"foo1":"rWRNY","foo2":12.3},"files":{},"form":{},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-61af602a-54fcb6412d2d064822bcdd5f","content-length":"28","user-agent":"Go-http-client/1.1","content-type":"application/json; charset=UTF-8","accept-encoding":"gzip"},"json":{"foo1":"rWRNY","foo2":12.3},"url":"https://postman-echo.com/post"}
-------------------------------------------------- --------------------------------------------------
8:04PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true 9:22PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true
8:04PM INF validate body.json.foo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true 9:22PM INF validate body.json.foo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
8:04PM INF validate body.json.foo2 assertMethod=equals checkValue=12.3 expectValue=12.3 result=true 9:22PM INF validate body.json.foo2 assertMethod=equals checkValue=12.3 expectValue=12.3 result=true
8:04PM INF run step end exportVars=null step="post json data" success=true 9:22PM INF run step end exportVars=null step="post json data" success=true
8:04PM INF run step start step="post form data" 9:22PM INF run step start step="post form data"
8:04PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3 9:22PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3
-------------------- request -------------------- -------------------- request --------------------
POST /post HTTP/1.1 POST /post HTTP/1.1
Host: postman-echo.com Host: postman-echo.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
foo1=B64R8&foo2=12.3 foo1=rWRNY&foo2=12.3
==================== response =================== ==================== response ===================
HTTP/1.1 200 OK HTTP/1.1 200 OK
Content-Length: 445 Content-Length: 445
Connection: keep-alive Connection: keep-alive
Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8
Date: Thu, 11 Nov 2021 12:04:32 GMT Date: Tue, 07 Dec 2021 13:22:50 GMT
Etag: W/"1bd-g/z+op+J2/U1DlrEv2g2VhZ0on4" Etag: W/"1bd-V7gWOjKCZvyBWVyqprN77w2dmXE"
Set-Cookie: sails.sid=s%3ALfq9XEgKVT4dKQ8PnxUJ9-WSq4wI96Po.2P90TP9V2Pje3GNJ1hJmLcRRgcQy%2FDwBPF63Xdvdq4o; Path=/; HttpOnly Set-Cookie: sails.sid=s%3Aj4sUA8hI4rAt9JMq1m4k_chSDlfkAEBV.ZfisF4bIH2e7iBY6%2BSHqUbHNBbhCzZi%2Fu4byLDdxy%2B4; Path=/; HttpOnly
Vary: Accept-Encoding Vary: Accept-Encoding
{"args":{},"data":"","files":{},"form":{"foo1":"B64R8","foo2":"12.3"},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-618d06d0-56d250242bf05b7144edf2cb","content-length":"20","user-agent":"Go-http-client/1.1","content-type":"application/x-www-form-urlencoded; charset=UTF-8","accept-encoding":"gzip"},"json":{"foo1":"B64R8","foo2":"12.3"},"url":"https://postman-echo.com/post"} {"args":{},"data":"","files":{},"form":{"foo1":"rWRNY","foo2":"12.3"},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-61af602a-2cc056eb54ba2f0c6850d84a","content-length":"20","user-agent":"Go-http-client/1.1","content-type":"application/x-www-form-urlencoded; charset=UTF-8","accept-encoding":"gzip"},"json":{"foo1":"rWRNY","foo2":"12.3"},"url":"https://postman-echo.com/post"}
-------------------------------------------------- --------------------------------------------------
8:04PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true 9:22PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true
8:04PM INF validate body.form.foo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true 9:22PM INF validate body.form.foo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
8:04PM INF validate body.form.foo2 assertMethod=equals checkValue=12.3 expectValue=12.3 result=true 9:22PM INF validate body.form.foo2 assertMethod=equals checkValue=12.3 expectValue=12.3 result=true
8:04PM INF run step end exportVars=null step="post form data" success=true 9:22PM INF run step end exportVars=null step="post form data" success=true
8:04PM INF run testcase end testcase="demo with complex mechanisms" 9:22PM INF run testcase end testcase="demo with complex mechanisms"
``` ```
</details> </details>
@@ -175,19 +175,17 @@ import (
func TestCaseDemo(t *testing.T) { func TestCaseDemo(t *testing.T) {
demoTestCase := &hrp.TestCase{ demoTestCase := &hrp.TestCase{
Config: hrp.TConfig{ Config: hrp.NewConfig("demo with complex mechanisms").
Name: "demo with complex mechanisms", SetBaseURL("https://postman-echo.com").
BaseURL: "https://postman-echo.com", WithVariables(map[string]interface{}{ // global level variables
Variables: map[string]interface{}{ // global level variables
"n": 5, "n": 5,
"a": 12.3, "a": 12.3,
"b": 3.45, "b": 3.45,
"varFoo1": "${gen_random_string($n)}", "varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function "varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
}, }),
},
TestSteps: []hrp.IStep{ TestSteps: []hrp.IStep{
hrp.Step("get with params"). hrp.NewStep("get with params").
WithVariables(map[string]interface{}{ // step level variables WithVariables(map[string]interface{}{ // step level variables
"n": 3, // inherit config level variables if not set in step level, a/varFoo1 "n": 3, // inherit config level variables if not set in step level, a/varFoo1
"b": 34.5, // override config level variable if existed, n/b/varFoo2 "b": 34.5, // override config level variable if existed, n/b/varFoo2
@@ -204,7 +202,7 @@ func TestCaseDemo(t *testing.T) {
AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath
AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
hrp.Step("post json data"). hrp.NewStep("post json data").
POST("/post"). POST("/post").
WithBody(map[string]interface{}{ WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable "foo1": "$varFoo1", // reference former extracted variable
@@ -214,7 +212,7 @@ func TestCaseDemo(t *testing.T) {
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 5, "check args foo1"). AssertLengthEqual("body.json.foo1", 5, "check args foo1").
AssertEqual("body.json.foo2", 12.3, "check args foo2"), AssertEqual("body.json.foo2", 12.3, "check args foo2"),
hrp.Step("post form data"). hrp.NewStep("post form data").
POST("/post"). POST("/post").
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}). WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
WithBody(map[string]interface{}{ WithBody(map[string]interface{}{

View File

@@ -8,25 +8,27 @@ import (
"github.com/httprunner/hrp/internal/ga" "github.com/httprunner/hrp/internal/ga"
) )
func NewStandaloneBoomer(spawnCount int, spawnRate float64) *Boomer { func NewStandaloneBoomer(spawnCount int, spawnRate float64) *hrpBoomer {
b := &Boomer{ b := &hrpBoomer{
Boomer: boomer.NewStandaloneBoomer(spawnCount, spawnRate), Boomer: boomer.NewStandaloneBoomer(spawnCount, spawnRate),
debug: false, debug: false,
} }
return b return b
} }
type Boomer struct { type hrpBoomer struct {
*boomer.Boomer *boomer.Boomer
debug bool debug bool
} }
func (b *Boomer) SetDebug(debug bool) *Boomer { // SetDebug configures whether to log HTTP request and response content.
func (b *hrpBoomer) SetDebug(debug bool) *hrpBoomer {
b.debug = debug b.debug = debug
return b return b
} }
func (b *Boomer) Run(testcases ...ITestCase) { // Run starts to run load test for one or multiple testcases.
func (b *hrpBoomer) Run(testcases ...ITestCase) {
event := ga.EventTracking{ event := ga.EventTracking{
Category: "RunLoadTests", Category: "RunLoadTests",
Action: "hrp boom", Action: "hrp boom",
@@ -48,11 +50,12 @@ func (b *Boomer) Run(testcases ...ITestCase) {
b.Boomer.Run(taskSlice...) b.Boomer.Run(taskSlice...)
} }
func (b *Boomer) Quit() { // Quit stops running load test.
func (b *hrpBoomer) Quit() {
b.Boomer.Quit() b.Boomer.Quit()
} }
func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task { func (b *hrpBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
return &boomer.Task{ return &boomer.Task{
Name: testcase.Config.Name, Name: testcase.Config.Name,
Weight: testcase.Config.Weight, Weight: testcase.Config.Weight,
@@ -65,9 +68,9 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
stepData, err := runner.runStep(step, config) stepData, err := runner.runStep(step, config)
elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond) elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond)
if err == nil { if err == nil {
b.RecordSuccess(step.getType(), step.name(), elapsed, stepData.responseLength) b.RecordSuccess(step.Type(), step.Name(), elapsed, stepData.responseLength)
} else { } else {
b.RecordFailure(step.getType(), step.name(), elapsed, err.Error()) b.RecordFailure(step.Type(), step.Name(), elapsed, err.Error())
} }
} }
}, },

View File

@@ -15,7 +15,7 @@ func (tc *TestCase) ToTCase() (*TCase, error) {
Config: tc.Config, Config: tc.Config,
} }
for _, step := range tc.TestSteps { for _, step := range tc.TestSteps {
tCase.TestSteps = append(tCase.TestSteps, step.toStruct()) tCase.TestSteps = append(tCase.TestSteps, step.ToStruct())
} }
return &tCase, nil return &tCase, nil
} }

View File

@@ -89,6 +89,7 @@
{ {
"check": "body.json", "check": "body.json",
"assert": "equals", "assert": "equals",
"expect": null,
"msg": "assert response body json" "msg": "assert response body json"
}, },
{ {
@@ -190,6 +191,7 @@
{ {
"check": "body.json", "check": "body.json",
"assert": "equals", "assert": "equals",
"expect": null,
"msg": "assert response body json" "msg": "assert response body json"
}, },
{ {
@@ -243,6 +245,7 @@
{ {
"check": "body.json", "check": "body.json",
"assert": "equals", "assert": "equals",
"expect": null,
"msg": "assert response body json" "msg": "assert response body json"
}, },
{ {
@@ -296,6 +299,7 @@
{ {
"check": "body.json", "check": "body.json",
"assert": "equals", "assert": "equals",
"expect": null,
"msg": "assert response body json" "msg": "assert response body json"
}, },
{ {

View File

@@ -63,6 +63,7 @@ teststeps:
msg: assert response body data msg: assert response body data
- check: body.json - check: body.json
assert: equals assert: equals
expect: null
msg: assert response body json msg: assert response body json
- check: body.url - check: body.url
assert: equals assert: equals
@@ -134,6 +135,7 @@ teststeps:
msg: assert response body data msg: assert response body data
- check: body.json - check: body.json
assert: equals assert: equals
expect: null
msg: assert response body json msg: assert response body json
- check: body.url - check: body.url
assert: equals assert: equals
@@ -171,6 +173,7 @@ teststeps:
msg: assert response body data msg: assert response body data
- check: body.json - check: body.json
assert: equals assert: equals
expect: null
msg: assert response body json msg: assert response body json
- check: body.url - check: body.url
assert: equals assert: equals
@@ -208,6 +211,7 @@ teststeps:
msg: assert response body data msg: assert response body data
- check: body.json - check: body.json
assert: equals assert: equals
expect: null
msg: assert response body json msg: assert response body json
- check: body.url - check: body.url
assert: equals assert: equals

View File

@@ -20,14 +20,14 @@ func (s *stepRequestExtraction) Validate() *stepRequestValidation {
} }
} }
func (s *stepRequestExtraction) name() string { func (s *stepRequestExtraction) Name() string {
return s.step.Name return s.step.Name
} }
func (s *stepRequestExtraction) getType() string { func (s *stepRequestExtraction) Type() string {
return fmt.Sprintf("request-%v", s.step.Request.Method) return fmt.Sprintf("request-%v", s.step.Request.Method)
} }
func (s *stepRequestExtraction) toStruct() *TStep { func (s *stepRequestExtraction) ToStruct() *TStep {
return s.step return s.step
} }

View File

@@ -12,36 +12,38 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/httprunner/hrp" "github.com/httprunner/hrp"
"github.com/httprunner/hrp/internal/ga" "github.com/httprunner/hrp/internal/ga"
) )
var log = hrp.GetLogger() var log zerolog.Logger
const ( const (
suffixJSON = ".json" suffixJSON = ".json"
suffixYAML = ".yaml" suffixYAML = ".yaml"
) )
func NewHAR(path string) *HAR { func NewHAR(path string) *har {
return &HAR{ log = hrp.GetLogger()
return &har{
path: path, path: path,
} }
} }
type HAR struct { type har struct {
path string path string
filterStr string filterStr string
excludeStr string excludeStr string
outputDir string outputDir string
} }
func (h *HAR) SetOutputDir(dir string) { func (h *har) SetOutputDir(dir string) {
h.outputDir = dir h.outputDir = dir
} }
func (h *HAR) GenJSON() (jsonPath string, err error) { func (h *har) GenJSON() (jsonPath string, err error) {
event := ga.EventTracking{ event := ga.EventTracking{
Category: "har2case", Category: "har2case",
Action: "hrp har2case --to-json", Action: "hrp har2case --to-json",
@@ -60,7 +62,7 @@ func (h *HAR) GenJSON() (jsonPath string, err error) {
return return
} }
func (h *HAR) GenYAML() (yamlPath string, err error) { func (h *har) GenYAML() (yamlPath string, err error) {
event := ga.EventTracking{ event := ga.EventTracking{
Category: "har2case", Category: "har2case",
Action: "hrp har2case --to-yaml", Action: "hrp har2case --to-yaml",
@@ -79,7 +81,7 @@ func (h *HAR) GenYAML() (yamlPath string, err error) {
return return
} }
func (h *HAR) makeTestCase() (*hrp.TCase, error) { func (h *har) makeTestCase() (*hrp.TCase, error) {
teststeps, err := h.prepareTestSteps() teststeps, err := h.prepareTestSteps()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -92,7 +94,7 @@ func (h *HAR) makeTestCase() (*hrp.TCase, error) {
return tCase, nil return tCase, nil
} }
func (h *HAR) load() (*Har, error) { func (h *har) load() (*Har, error) {
fp, err := os.Open(h.path) fp, err := os.Open(h.path)
if err != nil { if err != nil {
return nil, fmt.Errorf("open: %w", err) return nil, fmt.Errorf("open: %w", err)
@@ -113,12 +115,12 @@ func (h *HAR) load() (*Har, error) {
return har, nil return har, nil
} }
func (h *HAR) prepareConfig() *hrp.TConfig { func (h *har) prepareConfig() *hrp.TConfig {
return hrp.NewConfig("testcase description"). return hrp.NewConfig("testcase description").
SetVerifySSL(false) SetVerifySSL(false)
} }
func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) { func (h *har) prepareTestSteps() ([]*hrp.TStep, error) {
har, err := h.load() har, err := h.load()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -136,52 +138,52 @@ func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) {
return steps, nil return steps, nil
} }
func (h *HAR) prepareTestStep(entry *Entry) (*hrp.TStep, error) { func (h *har) prepareTestStep(entry *Entry) (*hrp.TStep, error) {
log.Info(). log.Info().
Str("method", entry.Request.Method). Str("method", entry.Request.Method).
Str("url", entry.Request.URL). Str("url", entry.Request.URL).
Msg("convert teststep") Msg("convert teststep")
tStep := &TStep{ step := &tStep{
TStep: hrp.TStep{ TStep: hrp.TStep{
Request: &hrp.Request{}, Request: &hrp.Request{},
Validators: make([]hrp.Validator, 0), Validators: make([]hrp.Validator, 0),
}, },
} }
if err := tStep.makeRequestMethod(entry); err != nil { if err := step.makeRequestMethod(entry); err != nil {
return nil, err return nil, err
} }
if err := tStep.makeRequestURL(entry); err != nil { if err := step.makeRequestURL(entry); err != nil {
return nil, err return nil, err
} }
if err := tStep.makeRequestParams(entry); err != nil { if err := step.makeRequestParams(entry); err != nil {
return nil, err return nil, err
} }
if err := tStep.makeRequestCookies(entry); err != nil { if err := step.makeRequestCookies(entry); err != nil {
return nil, err return nil, err
} }
if err := tStep.makeRequestHeaders(entry); err != nil { if err := step.makeRequestHeaders(entry); err != nil {
return nil, err return nil, err
} }
if err := tStep.makeRequestBody(entry); err != nil { if err := step.makeRequestBody(entry); err != nil {
return nil, err return nil, err
} }
if err := tStep.makeValidate(entry); err != nil { if err := step.makeValidate(entry); err != nil {
return nil, err return nil, err
} }
return &tStep.TStep, nil return &step.TStep, nil
} }
type TStep struct { type tStep struct {
hrp.TStep hrp.TStep
} }
func (s *TStep) makeRequestMethod(entry *Entry) error { func (s *tStep) makeRequestMethod(entry *Entry) error {
s.Request.Method = entry.Request.Method s.Request.Method = entry.Request.Method
return nil return nil
} }
func (s *TStep) makeRequestURL(entry *Entry) error { func (s *tStep) makeRequestURL(entry *Entry) error {
u, err := url.Parse(entry.Request.URL) u, err := url.Parse(entry.Request.URL)
if err != nil { if err != nil {
@@ -192,7 +194,7 @@ func (s *TStep) makeRequestURL(entry *Entry) error {
return nil return nil
} }
func (s *TStep) makeRequestParams(entry *Entry) error { func (s *tStep) makeRequestParams(entry *Entry) error {
s.Request.Params = make(map[string]interface{}) s.Request.Params = make(map[string]interface{})
for _, param := range entry.Request.QueryString { for _, param := range entry.Request.QueryString {
s.Request.Params[param.Name] = param.Value s.Request.Params[param.Name] = param.Value
@@ -200,7 +202,7 @@ func (s *TStep) makeRequestParams(entry *Entry) error {
return nil return nil
} }
func (s *TStep) makeRequestCookies(entry *Entry) error { func (s *tStep) makeRequestCookies(entry *Entry) error {
s.Request.Cookies = make(map[string]string) s.Request.Cookies = make(map[string]string)
for _, cookie := range entry.Request.Cookies { for _, cookie := range entry.Request.Cookies {
s.Request.Cookies[cookie.Name] = cookie.Value s.Request.Cookies[cookie.Name] = cookie.Value
@@ -208,7 +210,7 @@ func (s *TStep) makeRequestCookies(entry *Entry) error {
return nil return nil
} }
func (s *TStep) makeRequestHeaders(entry *Entry) error { func (s *tStep) makeRequestHeaders(entry *Entry) error {
s.Request.Headers = make(map[string]string) s.Request.Headers = make(map[string]string)
for _, header := range entry.Request.Headers { for _, header := range entry.Request.Headers {
if strings.EqualFold(header.Name, "cookie") { if strings.EqualFold(header.Name, "cookie") {
@@ -219,7 +221,7 @@ func (s *TStep) makeRequestHeaders(entry *Entry) error {
return nil return nil
} }
func (s *TStep) makeRequestBody(entry *Entry) error { func (s *tStep) makeRequestBody(entry *Entry) error {
mimeType := entry.Request.PostData.MimeType mimeType := entry.Request.PostData.MimeType
if mimeType == "" { if mimeType == "" {
// GET/HEAD/DELETE without body // GET/HEAD/DELETE without body
@@ -253,7 +255,7 @@ func (s *TStep) makeRequestBody(entry *Entry) error {
return nil return nil
} }
func (s *TStep) makeValidate(entry *Entry) error { func (s *tStep) makeValidate(entry *Entry) error {
// make validator for response status code // make validator for response status code
s.Validators = append(s.Validators, hrp.Validator{ s.Validators = append(s.Validators, hrp.Validator{
Check: "status_code", Check: "status_code",
@@ -329,7 +331,7 @@ func (s *TStep) makeValidate(entry *Entry) error {
return nil return nil
} }
func (h *HAR) genOutputPath(suffix string) string { func (h *har) genOutputPath(suffix string) string {
file := getFilenameWithoutExtension(h.path) + suffix file := getFilenameWithoutExtension(h.path) + suffix
if h.outputDir != "" { if h.outputDir != "" {
return filepath.Join(h.outputDir, file) return filepath.Join(h.outputDir, file)

View File

@@ -19,7 +19,7 @@ var boomCmd = &cobra.Command{
$ hrp boom examples/ # run testcases in specified folder`, $ hrp boom examples/ # run testcases in specified folder`,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
hrp.SetLogLevel("WARN") // disable info logs for load testing hrp.SetLogger("WARN", logJSON) // disable info logs for load testing
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var paths []hrp.ITestCase var paths []hrp.ITestCase

View File

@@ -1,9 +1,9 @@
package cmd package cmd
import ( import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/httprunner/hrp"
"github.com/httprunner/hrp/har2case" "github.com/httprunner/hrp/har2case"
) )
@@ -37,6 +37,7 @@ var har2caseCmd = &cobra.Command{
} }
outputFiles = append(outputFiles, outputPath) outputFiles = append(outputFiles, outputPath)
} }
log := hrp.GetLogger()
log.Info().Strs("output", outputFiles).Msg("convert testcase success") log.Info().Strs("output", outputFiles).Msg("convert testcase success")
return nil return nil
}, },

View File

@@ -14,16 +14,13 @@ import (
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "hrp", Use: "hrp",
Short: "One-stop solution for HTTP(S) testing.", Short: "One-stop solution for HTTP(S) testing.",
Long: `hrp (HttpRunner+) is the next generation for HttpRunner. Enjoy! ✨ 🚀 ✨ Long: `hrp (HttpRunner+) is the one-stop solution for HTTP(S) testing. Enjoy! ✨ 🚀 ✨
License: Apache-2.0 License: Apache-2.0
Github: https://github.com/httprunner/hrp Github: https://github.com/httprunner/hrp
Copyright 2021 debugtalk`, Copyright 2021 debugtalk`,
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
if !logJSON { hrp.SetLogger(logLevel, logJSON)
hrp.SetLogPretty()
}
hrp.SetLogLevel(logLevel)
}, },
Version: version.VERSION, Version: version.VERSION,
} }

14
log.go
View File

@@ -10,7 +10,15 @@ import (
var log = zlog.Logger var log = zlog.Logger
func SetLogLevel(level string) { // SetLogger configures the log level and format.
func SetLogger(level string, logJSON bool) {
if !logJSON {
setLogPretty()
}
setLogLevel(level)
}
func setLogLevel(level string) {
level = strings.ToUpper(level) level = strings.ToUpper(level)
log.Info().Msgf("Set log level to %s", level) log.Info().Msgf("Set log level to %s", level)
switch level { switch level {
@@ -29,9 +37,9 @@ func SetLogLevel(level string) {
} }
} }
func SetLogPretty() { func setLogPretty() {
log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Info().Msg("Set log to pretty console") log.Info().Msg("Set log to color console other than JSON format.")
} }
func GetLogger() zerolog.Logger { func GetLogger() zerolog.Logger {

View File

@@ -65,14 +65,14 @@ type TCase struct {
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"` TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
} }
// IStep represents interface for all types for teststeps // IStep represents interface for all types for teststeps.
type IStep interface { type IStep interface {
name() string Name() string
getType() string Type() string
toStruct() *TStep ToStruct() *TStep
} }
// ITestCase represents interface for all types for testcases // ITestCase represents interface for all types for testcases.
type ITestCase interface { type ITestCase interface {
ToTestCase() (*TestCase, error) ToTestCase() (*TestCase, error)
ToTCase() (*TCase, error) ToTCase() (*TCase, error)

View File

@@ -27,11 +27,11 @@ func Run(testcases ...ITestCase) error {
} }
// NewRunner constructs a new runner instance. // NewRunner constructs a new runner instance.
func NewRunner(t *testing.T) *runner { func NewRunner(t *testing.T) *hrpRunner {
if t == nil { if t == nil {
t = &testing.T{} t = &testing.T{}
} }
return &runner{ return &hrpRunner{
t: t, t: t,
debug: false, // default to turn off debug debug: false, // default to turn off debug
client: &http.Client{ client: &http.Client{
@@ -44,7 +44,7 @@ func NewRunner(t *testing.T) *runner {
} }
} }
type runner struct { type hrpRunner struct {
t *testing.T t *testing.T
debug bool debug bool
client *http.Client client *http.Client
@@ -52,14 +52,14 @@ type runner struct {
} }
// SetDebug configures whether to log HTTP request and response content. // SetDebug configures whether to log HTTP request and response content.
func (r *runner) SetDebug(debug bool) *runner { func (r *hrpRunner) SetDebug(debug bool) *hrpRunner {
log.Info().Bool("debug", debug).Msg("[init] SetDebug") log.Info().Bool("debug", debug).Msg("[init] SetDebug")
r.debug = debug r.debug = debug
return r return r
} }
// SetProxyUrl configures the proxy URL, which is usually used to capture HTTP packets for debugging. // SetProxyUrl configures the proxy URL, which is usually used to capture HTTP packets for debugging.
func (r *runner) SetProxyUrl(proxyUrl string) *runner { func (r *hrpRunner) SetProxyUrl(proxyUrl string) *hrpRunner {
log.Info().Str("proxyUrl", proxyUrl).Msg("[init] SetProxyUrl") log.Info().Str("proxyUrl", proxyUrl).Msg("[init] SetProxyUrl")
p, err := url.Parse(proxyUrl) p, err := url.Parse(proxyUrl)
if err != nil { if err != nil {
@@ -74,7 +74,7 @@ func (r *runner) SetProxyUrl(proxyUrl string) *runner {
} }
// Run starts to execute one or multiple testcases. // Run starts to execute one or multiple testcases.
func (r *runner) Run(testcases ...ITestCase) error { func (r *hrpRunner) Run(testcases ...ITestCase) error {
event := ga.EventTracking{ event := ga.EventTracking{
Category: "RunAPITests", Category: "RunAPITests",
Action: "hrp run", Action: "hrp run",
@@ -98,7 +98,7 @@ func (r *runner) Run(testcases ...ITestCase) error {
return nil return nil
} }
func (r *runner) runCase(testcase *TestCase) error { func (r *hrpRunner) runCase(testcase *TestCase) error {
config := testcase.Config config := testcase.Config
if err := r.parseConfig(config); err != nil { if err := r.parseConfig(config); err != nil {
return err return err
@@ -117,12 +117,12 @@ func (r *runner) runCase(testcase *TestCase) error {
return nil return nil
} }
func (r *runner) runStep(step IStep, config *TConfig) (stepResult *stepData, err error) { func (r *hrpRunner) runStep(step IStep, config *TConfig) (stepResult *stepData, err error) {
log.Info().Str("step", step.name()).Msg("run step start") log.Info().Str("step", step.Name()).Msg("run step start")
// copy step to avoid data racing // copy step to avoid data racing
copiedStep := &TStep{} copiedStep := &TStep{}
if err = copier.Copy(copiedStep, step.toStruct()); err != nil { if err = copier.Copy(copiedStep, step.ToStruct()); err != nil {
log.Error().Err(err).Msg("copy step data failed") log.Error().Err(err).Msg("copy step data failed")
return return
} }
@@ -167,14 +167,14 @@ func (r *runner) runStep(step IStep, config *TConfig) (stepResult *stepData, err
} }
log.Info(). log.Info().
Str("step", step.name()). Str("step", step.Name()).
Bool("success", stepResult.success). Bool("success", stepResult.success).
Interface("exportVars", stepResult.exportVars). Interface("exportVars", stepResult.exportVars).
Msg("run step end") Msg("run step end")
return return
} }
func (r *runner) runStepRequest(step *TStep) (stepResult *stepData, err error) { func (r *hrpRunner) runStepRequest(step *TStep) (stepResult *stepData, err error) {
stepResult = &stepData{ stepResult = &stepData{
name: step.Name, name: step.Name,
success: false, success: false,
@@ -339,7 +339,7 @@ func (r *runner) runStepRequest(step *TStep) (stepResult *stepData, err error) {
return return
} }
func (r *runner) runStepTestCase(step *TStep) (stepResult *stepData, err error) { func (r *hrpRunner) runStepTestCase(step *TStep) (stepResult *stepData, err error) {
stepResult = &stepData{ stepResult = &stepData{
name: step.Name, name: step.Name,
success: false, success: false,
@@ -349,7 +349,7 @@ func (r *runner) runStepTestCase(step *TStep) (stepResult *stepData, err error)
return return
} }
func (r *runner) parseConfig(config *TConfig) error { func (r *hrpRunner) parseConfig(config *TConfig) error {
// parse config variables // parse config variables
parsedVariables, err := parseVariables(config.Variables) parsedVariables, err := parseVariables(config.Variables)
if err != nil { if err != nil {
@@ -375,7 +375,7 @@ func (r *runner) parseConfig(config *TConfig) error {
return nil return nil
} }
func (r *runner) getSummary() *testCaseSummary { func (r *hrpRunner) getSummary() *testCaseSummary {
return &testCaseSummary{} return &testCaseSummary{}
} }

12
step.go
View File

@@ -231,18 +231,18 @@ func (s *requestWithOptionalArgs) Extract() *stepRequestExtraction {
} }
} }
func (s *requestWithOptionalArgs) name() string { func (s *requestWithOptionalArgs) Name() string {
if s.step.Name != "" { if s.step.Name != "" {
return s.step.Name return s.step.Name
} }
return fmt.Sprintf("%s %s", s.step.Request.Method, s.step.Request.URL) return fmt.Sprintf("%s %s", s.step.Request.Method, s.step.Request.URL)
} }
func (s *requestWithOptionalArgs) getType() string { func (s *requestWithOptionalArgs) Type() string {
return fmt.Sprintf("request-%v", s.step.Request.Method) return fmt.Sprintf("request-%v", s.step.Request.Method)
} }
func (s *requestWithOptionalArgs) toStruct() *TStep { func (s *requestWithOptionalArgs) ToStruct() *TStep {
return s.step return s.step
} }
@@ -263,17 +263,17 @@ func (s *testcaseWithOptionalArgs) Export(names ...string) *testcaseWithOptional
return s return s
} }
func (s *testcaseWithOptionalArgs) name() string { func (s *testcaseWithOptionalArgs) Name() string {
if s.step.Name != "" { if s.step.Name != "" {
return s.step.Name return s.step.Name
} }
return s.step.TestCase.Config.Name return s.step.TestCase.Config.Name
} }
func (s *testcaseWithOptionalArgs) getType() string { func (s *testcaseWithOptionalArgs) Type() string {
return "testcase" return "testcase"
} }
func (s *testcaseWithOptionalArgs) toStruct() *TStep { func (s *testcaseWithOptionalArgs) ToStruct() *TStep {
return s.step return s.step
} }

View File

@@ -9,18 +9,18 @@ type stepRequestValidation struct {
step *TStep step *TStep
} }
func (s *stepRequestValidation) name() string { func (s *stepRequestValidation) Name() string {
if s.step.Name != "" { if s.step.Name != "" {
return s.step.Name return s.step.Name
} }
return fmt.Sprintf("%s %s", s.step.Request.Method, s.step.Request.URL) return fmt.Sprintf("%s %s", s.step.Request.Method, s.step.Request.URL)
} }
func (s *stepRequestValidation) getType() string { func (s *stepRequestValidation) Type() string {
return fmt.Sprintf("request-%v", s.step.Request.Method) return fmt.Sprintf("request-%v", s.step.Request.Method)
} }
func (s *stepRequestValidation) toStruct() *TStep { func (s *stepRequestValidation) ToStruct() *TStep {
return s.step return s.step
} }