refactor: relocate hrp tests

This commit is contained in:
debugtalk
2022-03-27 11:35:41 +08:00
parent 30003077fc
commit 3f2d907c9d
11 changed files with 9 additions and 206 deletions

25
hrp/tests/compat_test.go Normal file
View File

@@ -0,0 +1,25 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
// generated by examples/data/har/demo.har using HttpRunner v3.1.6
var (
demoHttpRunnerJSONPath hrp.TestCasePath = "../../examples/hrp/demo_httprunner.json"
demoHttpRunnerYAMLPath hrp.TestCasePath = "../../examples/hrp/demo_httprunner.yaml"
)
func TestCompatTestCase(t *testing.T) {
err := hrp.NewRunner(t).Run(&demoHttpRunnerJSONPath)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
err = hrp.NewRunner(t).Run(&demoHttpRunnerYAMLPath)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}

84
hrp/tests/extract_test.go Normal file
View File

@@ -0,0 +1,84 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
// reference extracted variables for validation in the same step
func TestCaseExtractStepSingle(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
"expectedStatusCode": 200,
}).
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").
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("$statusCode", "$expectedStatusCode", "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 current step
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
},
}
err := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}
// reference extracted variables from previous step
func TestCaseExtractStepAssociation(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
}).
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").
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("$statusCode", 200, "check status code").
AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("$varFoo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
hrp.NewStep("post json data").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
Validate().
AssertEqual("status_code", "$statusCode", "check status code"). // assert with extracted variable from previous step
AssertEqual("$varFoo1", "bar1", "check json foo1"). // assert with extracted variable from previous step
AssertEqual("body.json.foo2", "bar2", "check json foo2"),
},
}
err := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}

View File

@@ -0,0 +1,49 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
func TestCaseCallFunction(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with functions").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
}).
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}", "foo3": "Foo3"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
Extract().
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
AssertEqual("body.args.foo2", "12.3", "check args foo2").
AssertTypeMatch("body.args.foo3", "str", "check args foo3 is type string").
AssertStringEqual("body.args.foo3", "foo3", "check args foo3 case-insensitivity").
AssertContains("body.args.foo3", "Foo", "check contains ").
AssertContainedBy("body.args.foo3", "this is Foo3 test", "check contained by"), // notice: request params value will be converted to string
hrp.NewStep("post json data with functions").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
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 := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}

View File

@@ -0,0 +1,56 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
const rendezvousTestJSONPath = "rendezvous_test.json"
var rendezvousTestcase = &hrp.TestCase{
Config: hrp.NewConfig("run request with functions").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
}),
TestSteps: []hrp.IStep{
hrp.NewStep("waiting for all users in the beginning").
Rendezvous("rendezvous0"),
hrp.NewStep("rendezvous before get").
Rendezvous("rendezvous1").
WithUserNumber(50).
WithTimeout(3000),
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
Extract().
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("status_code", 200, "check status code"),
hrp.NewStep("rendezvous before post").
Rendezvous("rendezvous2").
WithUserNumber(20).
WithTimeout(2000),
hrp.NewStep("post json data with functions").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 4, "check args foo1").
AssertEqual("body.json.foo2", "foo2", "check args foo2"),
hrp.NewStep("waiting for all users in the end").
Rendezvous("rendezvous3"),
},
}
func TestRendezvous(t *testing.T) {
err := hrp.NewRunner(t).Run(rendezvousTestcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}

74
hrp/tests/request_test.go Normal file
View File

@@ -0,0 +1,74 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
func TestCaseBasicRequest(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("request methods testcase in hardcode").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2"),
hrp.NewStep("post raw text").
POST("/post").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
"Content-Type": "text/plain",
}).
WithBody("This is expected to be sent back as part of response body.").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"),
hrp.NewStep("post form data").
POST("/post").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
"Content-Type": "application/x-www-form-urlencoded",
}).
WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.form.foo1", "bar1", "check form foo1").
AssertEqual("body.form.foo2", "bar2", "check form foo2"),
hrp.NewStep("post json data").
POST("/post").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
}).
WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.json.foo1", "bar1", "check json foo1").
AssertEqual("body.json.foo2", "bar2", "check json foo2"),
hrp.NewStep("put request").
PUT("/put").
WithHeaders(map[string]string{
"User-Agent": "HttpRunnerPlus",
"Content-Type": "text/plain",
}).
WithBody("This is expected to be sent back as part of response body.").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"),
},
}
err := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}

View File

@@ -0,0 +1,55 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
func TestCaseValidateStep(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with validation").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
"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.\"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\"", "HttpRunnerPlus", "check header user agent"),
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
}).
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 := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}

144
hrp/tests/variables_test.go Normal file
View File

@@ -0,0 +1,144 @@
package tests
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
func TestCaseConfigVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
"expectedStatusCode": 200,
}).SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
GET("/get").
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "$agent"}).
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
},
}
err := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}
func TestCaseStepVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1",
"agent": "HttpRunnerPlus",
"expectedStatusCode": 200,
}).
GET("/get").
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "$agent"}).
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
},
}
err := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}
func TestCaseOverrideConfigVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with variables").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"var1": "bar0",
"agent": "HttpRunnerPlus",
"expectedStatusCode": 200,
}).SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"var1": "bar1", // override config variable
"agent": "$agent", // reference config variable
// expectedStatusCode, inherit config variable
}).
GET("/get").
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "$agent"}).
Validate().
AssertEqual("status_code", "$expectedStatusCode", "check status code").
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
AssertEqual("body.args.foo1", "bar1", "check args foo1").
AssertEqual("body.args.foo2", "bar2", "check args foo2").
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
},
}
err := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}
func TestCaseParseVariables(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with functions").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3
}).SetVerifySSL(false),
TestSteps: []hrp.IStep{
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{
"n": 3,
"b": 34.5,
"varFoo2": "${max($a, $b)}", // 34.5
}).
GET("/get").
WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
Extract().
WithJmesPath("body.args.foo1", "varFoo1").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
hrp.NewStep("post json data with functions").
POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
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 := hrp.NewRunner(t).Run(testcase)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
}