diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 506ef68c..19770c9f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,7 +2,7 @@ ## v0.3.0 (2021-12-22) -- feat: implement transaction mechanism for load test +- feat: implement `transaction` mechanism for load test - feat: support `--continue-on-failure` flag to continue running next step when failure occurs, default to failfast - refactor: fork [boomer] as sub module - feat: report GA events with version diff --git a/examples/demo.json b/examples/demo.json index 4f371a05..0f6c9819 100644 --- a/examples/demo.json +++ b/examples/demo.json @@ -11,6 +11,13 @@ } }, "teststeps": [ + { + "name": "transaction 1 start", + "transaction": { + "name": "tran1", + "type": "start" + } + }, { "name": "get with params", "request": { @@ -64,6 +71,13 @@ } ] }, + { + "name": "transaction 1 end", + "transaction": { + "name": "tran1", + "type": "end" + } + }, { "name": "post json data", "request": { diff --git a/examples/demo.yaml b/examples/demo.yaml index 0bc18920..a0cee432 100644 --- a/examples/demo.yaml +++ b/examples/demo.yaml @@ -8,6 +8,10 @@ config: varFoo1: ${gen_random_string($n)} varFoo2: ${max($a, $b)} teststeps: + - name: transaction 1 start + transaction: + name: tran1 + type: start - name: get with params request: method: GET @@ -43,6 +47,10 @@ teststeps: assert: equals expect: "34.5" msg: check args foo2 + - name: transaction 1 end + transaction: + name: tran1 + type: end - name: post json data request: method: POST diff --git a/examples/demo_test.go b/examples/demo_test.go index 0fb4e566..bb41544e 100644 --- a/examples/demo_test.go +++ b/examples/demo_test.go @@ -18,6 +18,7 @@ var demoTestCase = &hrp.TestCase{ "varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function }), TestSteps: []hrp.IStep{ + hrp.NewStep("transaction 1 start").StartTransaction("tran1"), // start transaction 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 @@ -35,6 +36,7 @@ var demoTestCase = &hrp.TestCase{ 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.NewStep("transaction 1 end").EndTransaction("tran1"), // end transaction hrp.NewStep("post json data"). POST("/post"). WithBody(map[string]interface{}{ diff --git a/examples/demo_test.py b/examples/demo_test.py new file mode 100644 index 00000000..e2eddc1f --- /dev/null +++ b/examples/demo_test.py @@ -0,0 +1,63 @@ +# NOTE: Generated By HttpRunner v3.1.6 +# FROM: hrp/examples/demo.json + + +from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase + + +class TestCaseDemo(HttpRunner): + + config = ( + Config("demo with complex mechanisms") + .variables( + **{ + "a": 12.3, + "b": 3.45, + "n": 5, + "varFoo1": "${gen_random_string($n)}", + "varFoo2": "${max($a, $b)}", + } + ) + .base_url("https://postman-echo.com") + ) + + teststeps = [ + Step( + RunRequest("get with params") + .with_variables(**{"b": 34.5, "n": 3, "varFoo2": "${max($a, $b)}"}) + .get("/get") + .with_params(**{"foo1": "$varFoo1", "foo2": "$varFoo2"}) + .with_headers(**{"User-Agent": "HttpRunnerPlus"}) + .extract() + .with_jmespath("body.args.foo1", "varFoo1") + .validate() + .assert_equal("status_code", 200) + .assert_equal('headers."Content-Type"', "application/json") + .assert_equal("body.args.foo1", 5) + .assert_equal("$varFoo1", 5) + .assert_equal("body.args.foo2", "34.5") + ), + Step( + RunRequest("post json data") + .post("/post") + .validate() + .assert_equal("status_code", 200) + .assert_equal("body.json.foo1", 5) + .assert_equal("body.json.foo2", 12.3) + ), + Step( + RunRequest("post form data") + .post("/post") + .with_headers( + **{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} + ) + .validate() + .assert_equal("status_code", 200) + .assert_equal("body.form.foo1", 5) + .assert_equal("body.form.foo2", "12.3") + ), + ] + + +if __name__ == "__main__": + TestCaseDemo().test_start() diff --git a/step.go b/step.go index 5cbc296e..4bfcc167 100644 --- a/step.go +++ b/step.go @@ -174,6 +174,28 @@ func (s *StepRequest) CallRefCase(tc *TestCase) *StepTestCaseWithOptionalArgs { } } +// StartTransaction starts a transaction. +func (s *StepRequest) StartTransaction(name string) *StepTransaction { + s.step.Transaction = &Transaction{ + Name: name, + Type: TransactionStart, + } + return &StepTransaction{ + step: s.step, + } +} + +// EndTransaction ends a transaction. +func (s *StepRequest) EndTransaction(name string) *StepTransaction { + s.step.Transaction = &Transaction{ + Name: name, + Type: TransactionEnd, + } + return &StepTransaction{ + step: s.step, + } +} + // StepRequestWithOptionalArgs implements IStep interface. type StepRequestWithOptionalArgs struct { step *TStep