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

View File

@@ -1,25 +0,0 @@
package examples
import (
"testing"
"github.com/httprunner/httprunner/hrp"
)
// generated by examples/data/har/demo.har using HttpRunner v3.1.6
var (
demoHttpRunnerJSONPath hrp.TestCasePath = "demo_httprunner.json"
demoHttpRunnerYAMLPath hrp.TestCasePath = "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)
}
}

View File

@@ -1,63 +0,0 @@
# 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()

View File

@@ -1,84 +0,0 @@
package examples
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

@@ -1,49 +0,0 @@
package examples
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

@@ -1,51 +0,0 @@
{
"config": {
"name": "testcase description",
"variables": {},
"verify": false
},
"teststeps": [
{
"name": "/get",
"request": {
"url": "http://httpbin.org/get",
"method": "GET",
"headers": {
"Host": "httpbin.org",
"Connection": "keep-alive",
"accept": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50",
"Referer": "http://httpbin.org/",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
}
},
"validate": [
{
"check": "status_code",
"assert": "equals",
"expect": 200,
"msg": "assert response status code"
},
{
"check": "headers.\"Content-Type\"",
"assert": "equals",
"expect": "application/json",
"msg": "assert response header Content-Type"
},
{
"check": "body.origin",
"assert": "equals",
"expect": "117.176.133.109",
"msg": "assert response body origin"
},
{
"check": "body.url",
"assert": "equals",
"expect": "http://httpbin.org/get",
"msg": "assert response body url"
}
]
}
]
}

View File

@@ -1,65 +0,0 @@
package main
import (
"fmt"
"log"
)
func init() {
log.Println("plugin init function called")
}
func SumTwoInt(a, b int) int {
return a + b
}
func SumInts(args ...int) int {
var sum int
for _, arg := range args {
sum += arg
}
return sum
}
func Sum(args ...interface{}) (interface{}, error) {
var sum float64
for _, arg := range args {
switch v := arg.(type) {
case int:
sum += float64(v)
case float64:
sum += v
default:
return nil, fmt.Errorf("unexpected type: %T", arg)
}
}
return sum, nil
}
func SumTwoString(a, b string) string {
return a + b
}
func SumStrings(s ...string) string {
var sum string
for _, arg := range s {
sum += arg
}
return sum
}
func Concatenate(args ...interface{}) (interface{}, error) {
var result string
for _, arg := range args {
result += fmt.Sprintf("%v", arg)
}
return result, nil
}
func SetupHookExample(args string) string {
return fmt.Sprintf("step name: %v, setup...", args)
}
func TeardownHookExample(args string) string {
return fmt.Sprintf("step name: %v, teardown...", args)
}

View File

@@ -1,18 +0,0 @@
package main
import (
"github.com/httprunner/funplugin/fungo"
)
// register functions and build to plugin binary
func main() {
fungo.Register("sum_ints", SumInts)
fungo.Register("sum_two_int", SumTwoInt)
fungo.Register("sum", Sum)
fungo.Register("sum_two_string", SumTwoString)
fungo.Register("sum_strings", SumStrings)
fungo.Register("concatenate", Concatenate)
fungo.Register("setup_hook_example", SetupHookExample)
fungo.Register("teardown_hook_example", TeardownHookExample)
fungo.Serve()
}

View File

@@ -1,56 +0,0 @@
package examples
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)
}
}

View File

@@ -1,74 +0,0 @@
package examples
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

@@ -1,55 +0,0 @@
package examples
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)
}
}

View File

@@ -1,144 +0,0 @@
package examples
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)
}
}