From 6c042dcb68ca048a53b6e10665fa46e9a468f8ba Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 4 Oct 2021 18:39:34 +0800 Subject: [PATCH] docs: add example --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++ examples/demo_test.go | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 examples/demo_test.go diff --git a/README.md b/README.md index 4cf7a807..d0902090 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,75 @@ HttpBoomer is a golang implementation of [HttpRunner]. Ideally, HttpBoomer will ## Quick Start +### Install + +```bash +$ go get -u github.com/httprunner/httpboomer +``` + +### Examples + +This is an example of HttpBoomer testcase. You can find more in the [`examples`][examples] directory. + +```go + +import ( + "testing" + + "github.com/httprunner/httpboomer" +) + +func TestCaseDemo(t *testing.T) { + testcase := &httpboomer.TestCase{ + Config: httpboomer.TConfig{ + Name: "demo with complex mechanisms", + BaseURL: "https://postman-echo.com", + Variables: 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: []httpboomer.IStep{ + httpboomer.Step("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 + "varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again + }). + GET("/get"). + WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). // request with headers + Extract(). + WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath + Validate(). + AssertEqual("status_code", 200, "check response status code"). // validate response status code + AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header + 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 + httpboomer.Step("post json data"). + POST("/post"). + WithJSON(map[string]interface{}{ + "foo1": "$varFoo1", // reference former extracted variable + "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here + }). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertLengthEqual("body.json.foo1", 5, "check args foo1"). + AssertEqual("body.json.foo2", 12.3, "check args foo2"), + }, + } + + err := httpboomer.Test(t, testcase) + if err != nil { + t.Fatalf("run testcase error: %v", err) + } +} +``` + [HttpRunner]: https://github.com/httprunner/httprunner [Boomer]: https://github.com/myzhan/boomer [locust]: https://github.com/locustio/locust @@ -29,3 +98,4 @@ HttpBoomer is a golang implementation of [HttpRunner]. Ideally, HttpBoomer will [allure]: https://docs.qameta.io/allure/ [HAR]: http://httparchive.org/ [plugin]: https://pkg.go.dev/plugin +[examples]: examples/ diff --git a/examples/demo_test.go b/examples/demo_test.go new file mode 100644 index 00000000..f421b3bd --- /dev/null +++ b/examples/demo_test.go @@ -0,0 +1,57 @@ +package examples + +import ( + "testing" + + "github.com/httprunner/httpboomer" +) + +func TestCaseDemo(t *testing.T) { + testcase := &httpboomer.TestCase{ + Config: httpboomer.TConfig{ + Name: "demo with complex mechanisms", + BaseURL: "https://postman-echo.com", + Variables: 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: []httpboomer.IStep{ + httpboomer.Step("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 + "varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again + }). + GET("/get"). + WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). // request with headers + Extract(). + WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath + Validate(). + AssertEqual("status_code", 200, "check response status code"). // validate response status code + AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header + 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 + httpboomer.Step("post json data"). + POST("/post"). + WithJSON(map[string]interface{}{ + "foo1": "$varFoo1", // reference former extracted variable + "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here + }). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertLengthEqual("body.json.foo1", 5, "check args foo1"). + AssertEqual("body.json.foo2", 12.3, "check args foo2"), + }, + } + + err := httpboomer.Test(t, testcase) + if err != nil { + t.Fatalf("run testcase error: %v", err) + } +}