mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
change: add example for validation
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package postman_echo
|
||||
package examples
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
58
examples/validate_test.go
Normal file
58
examples/validate_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package examples
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/httprunner/httpboomer"
|
||||
)
|
||||
|
||||
func TestCaseValidateStep(t *testing.T) {
|
||||
testcase := &httpboomer.TestCase{
|
||||
Config: httpboomer.TConfig{
|
||||
Name: "run request with variables",
|
||||
BaseURL: "https://postman-echo.com",
|
||||
Verify: false,
|
||||
},
|
||||
TestSteps: []httpboomer.IStep{
|
||||
httpboomer.Step("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpBoomer",
|
||||
"expectedStatusCode": 200,
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Extract().
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("status_code", "$expectedStatusCode", "check status code"). // assert status code
|
||||
AssertEqual("headers.Connection", "keep-alive", "check header Connection"). // assert response header
|
||||
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). // assert response header, with double quotes
|
||||
AssertEqual("body.args.foo1", "bar1", "check args foo1"). // assert response json body with jmespath
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpBoomer", "check header user agent"),
|
||||
httpboomer.Step("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpBoomer",
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Extract().
|
||||
WithJmesPath("status_code", "statusCode").
|
||||
WithJmesPath("headers.\"Content-Type\"", "contentType").
|
||||
Validate().
|
||||
AssertEqual("$statusCode", 200, "check status code"). // assert with extracted variable from current step
|
||||
AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). // assert with extracted variable from current step
|
||||
AssertEqual("$varFoo1", "bar1", "check args foo1"). // assert with extracted variable from previous step
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2"), // assert response json body with jmespath
|
||||
},
|
||||
}
|
||||
|
||||
err := httpboomer.Test(t, testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package postman_echo
|
||||
package examples
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
Reference in New Issue
Block a user