mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
Merge pull request #4 from xucong053/master
feat: running with data-driven tests.
This commit is contained in:
12
boomer.go
12
boomer.go
@@ -45,8 +45,16 @@ func (b *hrpBoomer) Run(testcases ...ITestCase) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
task := b.convertBoomerTask(testcase)
|
||||
taskSlice = append(taskSlice, task)
|
||||
cfg := testcase.Config.ToStruct()
|
||||
parameters := getParameters(testcase.Config)
|
||||
if parameters == nil {
|
||||
parameters = []map[string]interface{}{{}}
|
||||
}
|
||||
for _, parameter := range parameters {
|
||||
cfg.Variables = mergeVariables(parameter, cfg.Variables)
|
||||
task := b.convertBoomerTask(testcase)
|
||||
taskSlice = append(taskSlice, task)
|
||||
}
|
||||
}
|
||||
b.Boomer.Run(taskSlice...)
|
||||
}
|
||||
|
||||
32
convert.go
32
convert.go
@@ -2,10 +2,12 @@ package hrp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -94,6 +96,36 @@ func loadFromYAML(path string) (*TCase, error) {
|
||||
return tc, err
|
||||
}
|
||||
|
||||
func loadFromCSV(path string) []map[string]string {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Error().Str("path", path).Err(err).Msg("convert absolute path failed")
|
||||
panic(err)
|
||||
}
|
||||
log.Info().Str("path", path).Msg("load csv file")
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("load csv file failed")
|
||||
panic(err)
|
||||
}
|
||||
r := csv.NewReader(strings.NewReader(string(file)))
|
||||
content, err := r.ReadAll()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("parse csv file failed")
|
||||
panic(err)
|
||||
}
|
||||
var result []map[string]string
|
||||
for i := 1; i < len(content); i++ {
|
||||
row := make(map[string]string)
|
||||
for j := 0; j < len(content[i]); j++ {
|
||||
row[content[0][j]] = content[i][j]
|
||||
}
|
||||
result = append(result, row)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (tc *TCase) ToTestCase() (*TestCase, error) {
|
||||
testCase := &TestCase{
|
||||
Config: &Config{cfg: tc.Config},
|
||||
|
||||
4
examples/account.csv
Normal file
4
examples/account.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
username,password
|
||||
test1,111111
|
||||
test2,222222
|
||||
test3,333333
|
||||
|
57
examples/parameters_test.json
Normal file
57
examples/parameters_test.json
Normal file
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"config": {
|
||||
"name": "request methods testcase: validate with parameters",
|
||||
"parameters": {
|
||||
"user_agent": [
|
||||
"iOS/10.1",
|
||||
"iOS/10.2"
|
||||
],
|
||||
"username-password": "${parameterize(examples/account.csv)}",
|
||||
"app_version": "${getAppVersion()}"
|
||||
},
|
||||
"parameters_setting": {
|
||||
"strategy": "random"
|
||||
},
|
||||
"variables": {
|
||||
"app_version": "f1"
|
||||
},
|
||||
"base_url": "https://postman-echo.com",
|
||||
"verify": false
|
||||
},
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "get with params",
|
||||
"variables": {
|
||||
"foo1": "$username",
|
||||
"foo2": "$password",
|
||||
"foo3": "$app_version"
|
||||
},
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "/get",
|
||||
"params": {
|
||||
"foo1": "$foo1",
|
||||
"foo2": "$foo2",
|
||||
"foo3": "$foo3"
|
||||
},
|
||||
"headers": {
|
||||
"User-Agent": "$user_agent,$app_version"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{
|
||||
"check": "status_code",
|
||||
"assert": "equals",
|
||||
"expect": 200,
|
||||
"msg": "check status code"
|
||||
},
|
||||
{
|
||||
"check": "body.args.foo3",
|
||||
"assert": "not_equal",
|
||||
"expect": "f1",
|
||||
"msg": "check app version"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
38
examples/parameters_test.yaml
Normal file
38
examples/parameters_test.yaml
Normal file
@@ -0,0 +1,38 @@
|
||||
config:
|
||||
name: "request methods testcase: validate with parameters"
|
||||
parameters:
|
||||
user_agent: ["iOS/10.1", "iOS/10.2"]
|
||||
username-password: ${parameterize(examples/account.csv)}
|
||||
app_version: ${getAppVersion()}
|
||||
parameters_setting:
|
||||
strategy: random
|
||||
variables:
|
||||
app_version: f1
|
||||
base_url: "https://postman-echo.com"
|
||||
verify: False
|
||||
|
||||
teststeps:
|
||||
-
|
||||
name: get with params
|
||||
variables:
|
||||
foo1: $username
|
||||
foo2: $password
|
||||
foo3: $app_version
|
||||
request:
|
||||
method: GET
|
||||
url: /get
|
||||
params:
|
||||
foo1: $foo1
|
||||
foo2: $foo2
|
||||
foo3: $foo3
|
||||
headers:
|
||||
User-Agent: $user_agent,$app_version
|
||||
validate:
|
||||
- check: status_code
|
||||
assert: equals
|
||||
expect: 200
|
||||
msg: check status code
|
||||
- check: body.args.foo3
|
||||
assert: not_equal
|
||||
expect: f1
|
||||
msg: check app version
|
||||
@@ -14,6 +14,7 @@ var Functions = map[string]interface{}{
|
||||
"gen_random_string": genRandomString, // call with one argument
|
||||
"max": math.Max, // call with two arguments
|
||||
"md5": MD5,
|
||||
"getAppVersion": getAppVersion, // test
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -44,3 +45,7 @@ func MD5(str string) string {
|
||||
hasher.Write([]byte(str))
|
||||
return hex.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
func getAppVersion() []float64 {
|
||||
return []float64{3.1, 3.3}
|
||||
}
|
||||
|
||||
15
models.go
15
models.go
@@ -13,13 +13,14 @@ const (
|
||||
// TConfig represents config data structure for testcase.
|
||||
// Each testcase should contain one config part.
|
||||
type TConfig struct {
|
||||
Name string `json:"name" yaml:"name"` // required
|
||||
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||
Name string `json:"name" yaml:"name"` // required
|
||||
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||
ParametersSetting map[string]interface{} `json:"parameters_setting,omitempty" yaml:"parameters_setting,omitempty"`
|
||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||
}
|
||||
|
||||
// Request represents HTTP request data structure.
|
||||
|
||||
165
parser.go
165
parser.go
@@ -3,12 +3,15 @@ package hrp
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/maja42/goval"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
@@ -247,15 +250,35 @@ func mergeVariables(variables, overriddenVariables map[string]interface{}) map[s
|
||||
return mergedVariables
|
||||
}
|
||||
|
||||
// callFunc call function with arguments
|
||||
// only support return at most one result value
|
||||
func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
function, ok := builtin.Functions[funcName]
|
||||
if !ok {
|
||||
func contains(s []string, e string) bool {
|
||||
for _, a := range s {
|
||||
if strings.EqualFold(a, e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getMappingFunction(funcName string) (interface{}, error) {
|
||||
if function, ok := builtin.Functions[funcName]; ok {
|
||||
// function is builtin
|
||||
return function, nil
|
||||
} else if contains([]string{"parameterize", "P"}, funcName) {
|
||||
// parameterize function
|
||||
return loadFromCSV, nil
|
||||
} else {
|
||||
// function not found
|
||||
return nil, fmt.Errorf("function %s is not found", funcName)
|
||||
}
|
||||
}
|
||||
|
||||
// callFunc call function with arguments
|
||||
// only support return at most one result value
|
||||
func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
function, err := getMappingFunction(funcName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
funcValue := reflect.ValueOf(function)
|
||||
if funcValue.Kind() != reflect.Func {
|
||||
// function not valid
|
||||
@@ -493,3 +516,135 @@ func findallVariables(raw string) variableSet {
|
||||
|
||||
return varSet
|
||||
}
|
||||
|
||||
func shuffleCartesianProduct(slice []map[string]interface{}) {
|
||||
if slice == nil || len(slice) == 0 {
|
||||
return
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().Unix()))
|
||||
for len(slice) > 0 {
|
||||
n := len(slice)
|
||||
randIndex := r.Intn(n)
|
||||
slice[n-1], slice[randIndex] = slice[randIndex], slice[n-1]
|
||||
slice = slice[:n-1]
|
||||
}
|
||||
}
|
||||
|
||||
func genCartesianProduct(params [][]map[string]interface{}) []map[string]interface{} {
|
||||
if params == nil || len(params) == 0 {
|
||||
return nil
|
||||
}
|
||||
var cartesianProduct []map[string]interface{}
|
||||
cartesianProduct = params[0]
|
||||
for i := 0; i < len(params)-1; i++ {
|
||||
var tempProduct []map[string]interface{}
|
||||
for _, param1 := range cartesianProduct {
|
||||
for _, param2 := range params[i+1] {
|
||||
tempProduct = append(tempProduct, mergeVariables(param1, param2))
|
||||
}
|
||||
}
|
||||
cartesianProduct = tempProduct
|
||||
}
|
||||
return cartesianProduct
|
||||
}
|
||||
|
||||
func getParameters(config IConfig) []map[string]interface{} {
|
||||
cfg := config.ToStruct()
|
||||
// parse config parameters
|
||||
parsedParams, err := parseParameters(cfg.Parameters, cfg.Variables)
|
||||
if err != nil {
|
||||
log.Error().Interface("parameters", cfg.Parameters).Err(err).Msg("parse config parameters failed")
|
||||
}
|
||||
if cfg.ParametersSetting["strategy"] != nil && strings.ToLower(cfg.ParametersSetting["strategy"].(string)) == "random" {
|
||||
shuffleCartesianProduct(parsedParams)
|
||||
}
|
||||
return parsedParams
|
||||
}
|
||||
|
||||
func parseParameters(parameters map[string]interface{}, variablesMapping map[string]interface{}) ([]map[string]interface{}, error) {
|
||||
if parameters == nil || len(parameters) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var parsedParametersSlice [][]map[string]interface{}
|
||||
for k, v := range parameters {
|
||||
parameterNameSlice := strings.Split(k, "-")
|
||||
var parameterSlice []map[string]interface{}
|
||||
rawValue := reflect.ValueOf(v)
|
||||
switch rawValue.Kind() {
|
||||
case reflect.String:
|
||||
parsedParameterContent, err := parseData(rawValue.Interface(), variablesMapping)
|
||||
if err != nil {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parse parameter error")
|
||||
return nil, err
|
||||
}
|
||||
parsedParameterRawValue := reflect.ValueOf(parsedParameterContent)
|
||||
if parsedParameterRawValue.Kind() != reflect.Slice {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parsed parameter content should be Slice, got %v")
|
||||
return nil, errors.New("parsed parameter content should be Slice")
|
||||
}
|
||||
for i := 0; i < parsedParameterRawValue.Len(); i++ {
|
||||
parameterMap := make(map[string]interface{})
|
||||
// e.g.
|
||||
elem := reflect.ValueOf(parsedParameterRawValue.Index(i).Interface())
|
||||
if elem.Kind() == reflect.Map {
|
||||
// e.g. [{"username": "test1", "password": "passwd1", "other": "111"}, {"username": "test2", "password": "passwd2", "other": ""222}]
|
||||
// -> [{"username": "test1", "password": "passwd1"}, {"username": "test2", "password": "passwd2"}] (username, password in parameterNameSlice)
|
||||
for _, key := range parameterNameSlice {
|
||||
if _, ok := elem.Interface().(map[string]string)[key]; ok {
|
||||
parameterMap[key] = elem.MapIndex(reflect.ValueOf(key)).Interface()
|
||||
} else {
|
||||
log.Error().Interface("parameterNameSlice", parameterNameSlice).Msg("[parseParameters] parameter name not found")
|
||||
return nil, errors.New("parameter name not found")
|
||||
}
|
||||
}
|
||||
} else if elem.Kind() == reflect.Slice {
|
||||
// e.g. [["test1", "passwd1"], ["test2", "passwd2"]] -> [{"username": "test1", "password": "passwd1"}, {"username": "test2", "password": "passwd2"}]
|
||||
if len(parameterNameSlice) != elem.Len() {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter name Slice and parameter content Slice should have the same length")
|
||||
return nil, errors.New("parameter name Slice and parameter cjntent Slice should have the same length")
|
||||
} else {
|
||||
for j := 0; j < elem.Len(); j++ {
|
||||
parameterMap[parameterNameSlice[j]] = elem.Index(j).Interface()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// e.g. ${getAppVersion()} -> [3.1, 3.0] -> [{"app_version": 3.1}, {"app_version": 3.0}]
|
||||
if len(parameterNameSlice) != 1 {
|
||||
log.Error().Interface("parameterNameSlice", parameterNameSlice).Msg("[parseParameters] parameter name slice should have only one element when parameter content is string")
|
||||
return nil, errors.New("parameter name slice should have only one element when parameter content is string")
|
||||
}
|
||||
parameterMap[parameterNameSlice[0]] = elem.Interface()
|
||||
}
|
||||
parameterSlice = append(parameterSlice, parameterMap)
|
||||
}
|
||||
case reflect.Slice:
|
||||
for i := 0; i < rawValue.Len(); i++ {
|
||||
parameterMap := make(map[string]interface{})
|
||||
elem := reflect.ValueOf(rawValue.Index(i).Interface())
|
||||
if elem.Kind() == reflect.Slice {
|
||||
// e.g. username-password: [["test1", "passwd1"], ["test2", "passwd2"]]
|
||||
if len(parameterNameSlice) != elem.Len() {
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter name Slice and parameter content Slice should have the same length")
|
||||
return nil, errors.New("parameter name Slice and parameter content Slice should have the same length")
|
||||
}
|
||||
for j := 0; j < elem.Len(); j++ {
|
||||
parameterMap[parameterNameSlice[j]] = elem.Index(j).Interface()
|
||||
}
|
||||
} else {
|
||||
// e.g. user_agent: ["iOS/10.1", "iOS/10.2"]
|
||||
if len(parameterNameSlice) != 1 {
|
||||
log.Error().Interface("parameterNameSlice", parameterNameSlice).Msg("[parseParameters] parameter name slice should have only one element when parameter content is string")
|
||||
return nil, errors.New("parameter name slice should have only one element when parameter content is string")
|
||||
}
|
||||
parameterMap[parameterNameSlice[0]] = elem.Interface()
|
||||
}
|
||||
parameterSlice = append(parameterSlice, parameterMap)
|
||||
}
|
||||
default:
|
||||
log.Error().Interface("parameter", parameters).Msg("[parseParameters] parameter content should be Slice or Text(variables or functions call)")
|
||||
return nil, errors.New("parameter content should be Slice or Text(variables or functions call)")
|
||||
}
|
||||
parsedParametersSlice = append(parsedParametersSlice, parameterSlice)
|
||||
}
|
||||
return genCartesianProduct(parsedParametersSlice), nil
|
||||
}
|
||||
|
||||
@@ -618,3 +618,56 @@ func TestFindallVariables(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseParameters(t *testing.T) {
|
||||
testData := []struct {
|
||||
rawVars map[string]interface{}
|
||||
expectVars []map[string]interface{}
|
||||
}{
|
||||
{
|
||||
map[string]interface{}{"username-password": "${parameterize(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||
[]map[string]interface{}{{"username": "test1", "password": "111111", "user_agent": "IOS/10.1"},
|
||||
{"username": "test1", "password": "111111", "user_agent": "IOS/10.2"},
|
||||
{"username": "test2", "password": "222222", "user_agent": "IOS/10.1"},
|
||||
{"username": "test2", "password": "222222", "user_agent": "IOS/10.2"},
|
||||
{"username": "test3", "password": "333333", "user_agent": "IOS/10.1"},
|
||||
{"username": "test3", "password": "333333", "user_agent": "IOS/10.2"}},
|
||||
},
|
||||
{
|
||||
map[string]interface{}{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, data := range testData {
|
||||
value, _ := parseParameters(data.rawVars, map[string]interface{}{})
|
||||
if !assert.Equal(t, data.expectVars, value) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseParametersError(t *testing.T) {
|
||||
testData := []struct {
|
||||
rawVars map[string]interface{}
|
||||
}{
|
||||
{
|
||||
map[string]interface{}{"username_password": "${parameterize(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||
},
|
||||
{
|
||||
map[string]interface{}{"username-password": "${parameterize(examples/account.csv)}", "user-agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||
},
|
||||
{
|
||||
map[string]interface{}{"username-password": "${param(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}},
|
||||
},
|
||||
}
|
||||
for _, data := range testData {
|
||||
_, err := parseParameters(data.rawVars, map[string]interface{}{})
|
||||
if !assert.Error(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
26
runner.go
26
runner.go
@@ -128,17 +128,24 @@ func (r *hrpRunner) runCase(testcase *TestCase) error {
|
||||
if err := r.parseConfig(config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := config.ToStruct()
|
||||
log.Info().Str("testcase", config.Name()).Msg("run testcase start")
|
||||
r.startTime = time.Now()
|
||||
for _, step := range testcase.TestSteps {
|
||||
_, err := r.runStep(step, config)
|
||||
if err != nil {
|
||||
if r.failfast {
|
||||
log.Error().Err(err).Msg("abort running due to failfast setting")
|
||||
return err
|
||||
parameters := getParameters(config)
|
||||
if parameters == nil {
|
||||
parameters = []map[string]interface{}{{}}
|
||||
}
|
||||
for _, parameter := range parameters {
|
||||
cfg.Variables = mergeVariables(parameter, cfg.Variables)
|
||||
r.startTime = time.Now()
|
||||
for _, step := range testcase.TestSteps {
|
||||
_, err := r.runStep(step, config)
|
||||
if err != nil {
|
||||
if r.failfast {
|
||||
log.Error().Err(err).Msg("abort running due to failfast setting")
|
||||
return err
|
||||
}
|
||||
log.Warn().Err(err).Msg("run step failed, continue next step")
|
||||
}
|
||||
log.Warn().Err(err).Msg("run step failed, continue next step")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,7 +476,6 @@ func (r *hrpRunner) parseConfig(config IConfig) error {
|
||||
return err
|
||||
}
|
||||
cfg.Variables = parsedVariables
|
||||
|
||||
// parse config name
|
||||
parsedName, err := parseString(cfg.Name, cfg.Variables)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user