change: demo testcase

This commit is contained in:
debugtalk
2021-11-11 16:02:06 +08:00
parent 7af418e704
commit 6bf56db88d
2 changed files with 103 additions and 174 deletions

View File

@@ -1,141 +1,36 @@
package hrp package hrp
import ( import (
"os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var demoTestCase = &TestCase{
Config: 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: []IStep{
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": "HttpRunnerPlus"}). // 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
Step("post json data").
POST("/post").
WithBody(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"),
Step("post form data").
POST("/post").
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
WithBody(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.form.foo1", 5, "check args foo1").
AssertEqual("body.form.foo2", "12.3", "check args foo2"), // form data will be converted to string
},
}
var ( var (
demoTestCaseJSONPath = "demo.json" demoTestCaseJSONPath = "examples/demo.json"
demoTestCaseYAMLPath = "demo.yaml" demoTestCaseYAMLPath = "examples/demo.yaml"
) )
func TestMain(m *testing.M) { func TestLoadCase(t *testing.T) {
tCase, _ := demoTestCase.ToTCase() tcJSON, err := loadFromJSON(demoTestCaseJSONPath)
// setup, prepare demo json/yaml testcase file path
err := tCase.Dump2JSON(demoTestCaseJSONPath)
if err != nil {
os.Exit(1)
}
err = tCase.Dump2YAML(demoTestCaseYAMLPath)
if err != nil {
os.Exit(1)
}
// run all tests
code := m.Run()
defer os.Exit(code)
// teardown
err = os.Remove(demoTestCaseJSONPath)
if err != nil {
os.Exit(1)
}
err = os.Remove(demoTestCaseYAMLPath)
if err != nil {
os.Exit(1)
}
}
func TestLoadJSONCase(t *testing.T) {
tc, err := loadFromJSON(demoTestCaseJSONPath)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.Fail() t.Fail()
} }
if !assert.Equal(t, tc.Config.Name, demoTestCase.Config.Name) { tcYAML, err := loadFromYAML(demoTestCaseYAMLPath)
t.Fail()
}
if !assert.Equal(t, tc.Config.BaseURL, demoTestCase.Config.BaseURL) {
t.Fail()
}
if !assert.Equal(t, tc.TestSteps[1].Name, demoTestCase.TestSteps[1].Name()) {
t.Fail()
}
if !assert.Equal(t, tc.TestSteps[1].Request, demoTestCase.TestSteps[1].ToStruct().Request) {
t.Fail()
}
}
func TestLoadYAMLCase(t *testing.T) {
tc, err := loadFromYAML(demoTestCaseYAMLPath)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.Fail() t.Fail()
} }
if !assert.Equal(t, tc.Config, demoTestCase.Config) {
t.Fail()
}
if !assert.Equal(t, tc.TestSteps[1].Name, demoTestCase.TestSteps[1].Name()) {
t.Fail()
}
if !assert.Equal(t, tc.TestSteps[1].Request, demoTestCase.TestSteps[1].ToStruct().Request) {
t.Fail()
}
}
func TestLoadJSONAndRun(t *testing.T) { if !assert.Equal(t, tcJSON.Config.Name, tcYAML.Config.Name) {
jsonPath := &TestCasePath{demoTestCaseJSONPath}
testcase, err := jsonPath.ToTestCase()
if !assert.NoError(t, err) {
t.Fail() t.Fail()
} }
err = Run(t, testcase) if !assert.Equal(t, tcJSON.Config.BaseURL, tcYAML.Config.BaseURL) {
if err != nil { t.Fail()
t.Fatalf("run testcase error: %v", err) }
if !assert.Equal(t, tcJSON.TestSteps[1].Name, tcYAML.TestSteps[1].Name) {
t.Fail()
}
if !assert.Equal(t, tcJSON.TestSteps[1].Request, tcYAML.TestSteps[1].Request) {
t.Fail()
} }
} }

View File

@@ -1,68 +1,102 @@
package examples package examples
import ( import (
"fmt"
"testing" "testing"
"github.com/httprunner/hrp" "github.com/httprunner/hrp"
) )
func TestCaseDemo(t *testing.T) { var demoTestCase = &hrp.TestCase{
testcase := &hrp.TestCase{ Config: hrp.TConfig{
Config: hrp.TConfig{ Name: "demo with complex mechanisms",
Name: "demo with complex mechanisms", BaseURL: "https://postman-echo.com",
BaseURL: "https://postman-echo.com", Variables: map[string]interface{}{ // global level variables
Variables: map[string]interface{}{ // global level variables "n": 5,
"n": 5, "a": 12.3,
"a": 12.3, "b": 3.45,
"b": 3.45, "varFoo1": "${gen_random_string($n)}",
"varFoo1": "${gen_random_string($n)}", "varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
},
}, },
TestSteps: []hrp.IStep{ },
hrp.Step("get with params"). TestSteps: []hrp.IStep{
WithVariables(map[string]interface{}{ // step level variables hrp.Step("get with params").
"n": 3, // inherit config level variables if not set in step level, a/varFoo1 WithVariables(map[string]interface{}{ // step level variables
"b": 34.5, // override config level variable if existed, n/b/varFoo2 "n": 3, // inherit config level variables if not set in step level, a/varFoo1
"varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again "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 GET("/get").
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). // request with headers WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params
Extract(). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). // request with headers
WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath Extract().
Validate(). WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath
AssertEqual("status_code", 200, "check response status code"). // validate response status code Validate().
AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header AssertEqual("status_code", 200, "check response status code"). // validate response status code
AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header
AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step
hrp.Step("post json data"). AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
POST("/post"). hrp.Step("post json data").
WithBody(map[string]interface{}{ POST("/post").
"foo1": "$varFoo1", // reference former extracted variable WithBody(map[string]interface{}{
"foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here "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"). Validate().
AssertLengthEqual("body.json.foo1", 5, "check args foo1"). AssertEqual("status_code", 200, "check status code").
AssertEqual("body.json.foo2", 12.3, "check args foo2"), AssertLengthEqual("body.json.foo1", 5, "check args foo1").
hrp.Step("post form data"). AssertEqual("body.json.foo2", 12.3, "check args foo2"),
POST("/post"). hrp.Step("post form data").
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}). POST("/post").
WithBody(map[string]interface{}{ WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
"foo1": "$varFoo1", // reference former extracted variable WithBody(map[string]interface{}{
"foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here "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"). Validate().
AssertLengthEqual("body.form.foo1", 5, "check args foo1"). AssertEqual("status_code", 200, "check status code").
AssertEqual("body.form.foo2", "12.3", "check args foo2"), // form data will be converted to string AssertLengthEqual("body.form.foo1", 5, "check args foo1").
}, AssertEqual("body.form.foo2", "12.3", "check args foo2"), // form data will be converted to string
} },
}
err := hrp.Run(t, testcase) var (
demoTestCaseJSONPath = "demo.json"
demoTestCaseYAMLPath = "demo.yaml"
)
func TestGenDemoTestCase(t *testing.T) {
tCase, _ := demoTestCase.ToTCase()
err := tCase.Dump2JSON(demoTestCaseJSONPath)
if err != nil { if err != nil {
t.Fatalf("run testcase error: %v", err) t.Fail()
}
err = tCase.Dump2YAML(demoTestCaseYAMLPath)
if err != nil {
t.Fail()
} }
} }
func Example_TestCase() {
err := hrp.NewRunner().Run(demoTestCase)
fmt.Println(err)
// Output:
// <nil>
}
func Example_JSONTestCase() {
testCase := &hrp.TestCasePath{Path: demoTestCaseJSONPath}
err := hrp.NewRunner().Run(testCase)
fmt.Println(err)
// Output:
// <nil>
}
func Example_YAMTestCase() {
testCase := &hrp.TestCasePath{Path: demoTestCaseYAMLPath}
err := hrp.NewRunner().Run(testCase)
fmt.Println(err)
// Output:
// <nil>
}