Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #35 from httprunner/refactor-models
Browse files Browse the repository at this point in the history
change: make API more concise
  • Loading branch information
debugtalk authored Dec 7, 2021
2 parents e7e62e7 + 2b3c1b3 commit 10795e0
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 137 deletions.
108 changes: 53 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Since installed, you will get a `hrp` command with multiple sub-commands.

```text
$ 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
Github: https://github.com/httprunner/hrp
Expand Down Expand Up @@ -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>

```text
8:04PM INF Set log to pretty console
8:04PM INF Set log level to INFO
8:04PM INF [init] SetDebug debug=true
8:04PM 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
8:04PM 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"
8:04PM INF call function success arguments=[12.3,34.5] funcName=max output=34.5
8:04PM INF run step start step="get with params"
9:22PM INF Set log to color console other than JSON format.
9:22PM INF Set log level to INFO
9:22PM INF [init] SetDebug debug=true
9:22PM INF load json testcase path=/Users/debugtalk/MyProjects/HttpRunner-dev/hrp/examples/demo.json
9:22PM INF call function success arguments=[5] funcName=gen_random_string output=rWRNY
9:22PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3
9:22PM INF run testcase start testcase="demo with complex mechanisms"
9:22PM 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 --------------------
GET /get?foo1=B64R8&foo2=34.5 HTTP/1.1
GET /get?foo1=rWRNY&foo2=34.5 HTTP/1.1
Host: postman-echo.com
User-Agent: HttpRunnerPlus
Expand All @@ -85,70 +85,70 @@ HTTP/1.1 200 OK
Content-Length: 304
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Date: Thu, 11 Nov 2021 12:04:32 GMT
Etag: W/"130-LUQ0LVU7KVSZha0O3nQxqPlr5dw"
Set-Cookie: sails.sid=s%3Ag6vZXrHHzs-B7Q1bFrYQq83dUje_EkSu.06vsqbkZvIOJ6mb1It7c6i354e%2B0t91K4cG14YFjSX0; Path=/; HttpOnly
Date: Tue, 07 Dec 2021 13:22:50 GMT
Etag: W/"130-gmtE0VWiyE0mXUGoJe5AyhMQ2ig"
Set-Cookie: sails.sid=s%3AEWPwP8H-nbpSrCseeulwDQ8OEtRy1pGu.aHV6KrEIiFgaJsUAuDmmmJCYiV6XkrHLS%2Fd9g9vtZQw; Path=/; HttpOnly
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
8:04PM INF set variable value=B64R8 variable=varFoo1
8:04PM 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
8:04PM INF validate body.args.foo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true
8:04PM INF validate $varFoo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true
8:04PM 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
8:04PM 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 extract value from=body.args.foo1 value=rWRNY
9:22PM INF set variable value=rWRNY variable=varFoo1
9:22PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true
9:22PM INF validate headers."Content-Type" assertMethod=startswith checkValue="application/json; charset=utf-8" expectValue=application/json result=true
9:22PM INF validate body.args.foo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
9:22PM INF validate $varFoo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
9:22PM INF validate body.args.foo2 assertMethod=equals checkValue=34.5 expectValue=34.5 result=true
9:22PM INF run step end exportVars={"varFoo1":"rWRNY"} step="get with params" success=true
9:22PM INF run step start step="post json data"
9:22PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3
-------------------- request --------------------
POST /post HTTP/1.1
Host: postman-echo.com
Content-Type: application/json; charset=UTF-8
{"foo1":"B64R8","foo2":12.3}
{"foo1":"rWRNY","foo2":12.3}
==================== response ===================
HTTP/1.1 200 OK
Content-Length: 424
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Date: Thu, 11 Nov 2021 12:04:32 GMT
Etag: W/"1a8-1umvYElau4WkHR7VON+jKXozT2c"
Set-Cookie: sails.sid=s%3AeNnS5IE6TBePzx95OfuwyIweJy5aExb0.7MH6Vb42vbZ6OhNT2nhQGcAmHgqcFmtM8X03Qsoxa1k; Path=/; HttpOnly
Date: Tue, 07 Dec 2021 13:22:50 GMT
Etag: W/"1a8-5fCAlcltnCS4Ed/6OxpH9i9dlKs"
Set-Cookie: sails.sid=s%3As1b8P7f8sc3JRNumS-XJrzbwb5oxdkOs.pXRRifddVUiWuzAxwBikBxf3ayM8OahgDDzP7kSnMCc; Path=/; HttpOnly
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
8:04PM INF validate body.json.foo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true
8:04PM 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
8:04PM 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 validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true
9:22PM INF validate body.json.foo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
9:22PM INF validate body.json.foo2 assertMethod=equals checkValue=12.3 expectValue=12.3 result=true
9:22PM INF run step end exportVars=null step="post json data" success=true
9:22PM INF run step start step="post form data"
9:22PM INF call function success arguments=[12.3,3.45] funcName=max output=12.3
-------------------- request --------------------
POST /post HTTP/1.1
Host: postman-echo.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
foo1=B64R8&foo2=12.3
foo1=rWRNY&foo2=12.3
==================== response ===================
HTTP/1.1 200 OK
Content-Length: 445
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Date: Thu, 11 Nov 2021 12:04:32 GMT
Etag: W/"1bd-g/z+op+J2/U1DlrEv2g2VhZ0on4"
Set-Cookie: sails.sid=s%3ALfq9XEgKVT4dKQ8PnxUJ9-WSq4wI96Po.2P90TP9V2Pje3GNJ1hJmLcRRgcQy%2FDwBPF63Xdvdq4o; Path=/; HttpOnly
Date: Tue, 07 Dec 2021 13:22:50 GMT
Etag: W/"1bd-V7gWOjKCZvyBWVyqprN77w2dmXE"
Set-Cookie: sails.sid=s%3Aj4sUA8hI4rAt9JMq1m4k_chSDlfkAEBV.ZfisF4bIH2e7iBY6%2BSHqUbHNBbhCzZi%2Fu4byLDdxy%2B4; Path=/; HttpOnly
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
8:04PM INF validate body.form.foo1 assertMethod=length_equals checkValue=B64R8 expectValue=5 result=true
8:04PM 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
8:04PM INF run testcase end testcase="demo with complex mechanisms"
9:22PM INF validate status_code assertMethod=equals checkValue=200 expectValue=200 result=true
9:22PM INF validate body.form.foo1 assertMethod=length_equals checkValue=rWRNY expectValue=5 result=true
9:22PM INF validate body.form.foo2 assertMethod=equals checkValue=12.3 expectValue=12.3 result=true
9:22PM INF run step end exportVars=null step="post form data" success=true
9:22PM INF run testcase end testcase="demo with complex mechanisms"
```
</details>

Expand All @@ -175,19 +175,17 @@ import (

func TestCaseDemo(t *testing.T) {
demoTestCase := &hrp.TestCase{
Config: hrp.TConfig{
Name: "demo with complex mechanisms",
BaseURL: "https://postman-echo.com",
Variables: map[string]interface{}{ // global level variables
Config: hrp.NewConfig("demo with complex mechanisms").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{ // global level variables
"n": 5,
"a": 12.3,
"b": 3.45,
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
},
},
}),
TestSteps: []hrp.IStep{
hrp.Step("get with params").
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{ // step level variables
"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
Expand All @@ -204,7 +202,7 @@ func TestCaseDemo(t *testing.T) {
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
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").
WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable
Expand All @@ -214,7 +212,7 @@ func TestCaseDemo(t *testing.T) {
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 5, "check args foo1").
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
hrp.Step("post form data").
hrp.NewStep("post form data").
POST("/post").
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
WithBody(map[string]interface{}{
Expand Down
21 changes: 12 additions & 9 deletions boomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ import (
"github.com/httprunner/hrp/internal/ga"
)

func NewStandaloneBoomer(spawnCount int, spawnRate float64) *Boomer {
b := &Boomer{
func NewStandaloneBoomer(spawnCount int, spawnRate float64) *hrpBoomer {
b := &hrpBoomer{
Boomer: boomer.NewStandaloneBoomer(spawnCount, spawnRate),
debug: false,
}
return b
}

type Boomer struct {
type hrpBoomer struct {
*boomer.Boomer
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
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{
Category: "RunLoadTests",
Action: "hrp boom",
Expand All @@ -48,11 +50,12 @@ func (b *Boomer) Run(testcases ...ITestCase) {
b.Boomer.Run(taskSlice...)
}

func (b *Boomer) Quit() {
// Quit stops running load test.
func (b *hrpBoomer) Quit() {
b.Boomer.Quit()
}

func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
func (b *hrpBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
return &boomer.Task{
Name: testcase.Config.Name,
Weight: testcase.Config.Weight,
Expand All @@ -65,9 +68,9 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
stepData, err := runner.runStep(step, config)
elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond)
if err == nil {
b.RecordSuccess(step.getType(), step.name(), elapsed, stepData.responseLength)
b.RecordSuccess(step.Type(), step.Name(), elapsed, stepData.responseLength)
} else {
b.RecordFailure(step.getType(), step.name(), elapsed, err.Error())
b.RecordFailure(step.Type(), step.Name(), elapsed, err.Error())
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (tc *TestCase) ToTCase() (*TCase, error) {
Config: tc.Config,
}
for _, step := range tc.TestSteps {
tCase.TestSteps = append(tCase.TestSteps, step.toStruct())
tCase.TestSteps = append(tCase.TestSteps, step.ToStruct())
}
return &tCase, nil
}
Expand Down
4 changes: 4 additions & 0 deletions examples/postman-echo.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
{
"check": "body.json",
"assert": "equals",
"expect": null,
"msg": "assert response body json"
},
{
Expand Down Expand Up @@ -190,6 +191,7 @@
{
"check": "body.json",
"assert": "equals",
"expect": null,
"msg": "assert response body json"
},
{
Expand Down Expand Up @@ -243,6 +245,7 @@
{
"check": "body.json",
"assert": "equals",
"expect": null,
"msg": "assert response body json"
},
{
Expand Down Expand Up @@ -296,6 +299,7 @@
{
"check": "body.json",
"assert": "equals",
"expect": null,
"msg": "assert response body json"
},
{
Expand Down
4 changes: 4 additions & 0 deletions examples/postman-echo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ teststeps:
msg: assert response body data
- check: body.json
assert: equals
expect: null
msg: assert response body json
- check: body.url
assert: equals
Expand Down Expand Up @@ -134,6 +135,7 @@ teststeps:
msg: assert response body data
- check: body.json
assert: equals
expect: null
msg: assert response body json
- check: body.url
assert: equals
Expand Down Expand Up @@ -171,6 +173,7 @@ teststeps:
msg: assert response body data
- check: body.json
assert: equals
expect: null
msg: assert response body json
- check: body.url
assert: equals
Expand Down Expand Up @@ -208,6 +211,7 @@ teststeps:
msg: assert response body data
- check: body.json
assert: equals
expect: null
msg: assert response body json
- check: body.url
assert: equals
Expand Down
6 changes: 3 additions & 3 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func (s *stepRequestExtraction) Validate() *stepRequestValidation {
}
}

func (s *stepRequestExtraction) name() string {
func (s *stepRequestExtraction) Name() string {
return s.step.Name
}

func (s *stepRequestExtraction) getType() string {
func (s *stepRequestExtraction) Type() string {
return fmt.Sprintf("request-%v", s.step.Request.Method)
}

func (s *stepRequestExtraction) toStruct() *TStep {
func (s *stepRequestExtraction) ToStruct() *TStep {
return s.step
}
Loading

0 comments on commit 10795e0

Please sign in to comment.