Merge branch 'master' of github.com:httprunner/httprunner

This commit is contained in:
buyuxiang
2022-05-25 15:43:19 +08:00
33 changed files with 160 additions and 44 deletions

View File

@@ -10,6 +10,7 @@ import (
func NewConfig(name string) *TConfig {
return &TConfig{
Name: name,
Environs: make(map[string]string),
Variables: make(map[string]interface{}),
}
}
@@ -19,9 +20,10 @@ func NewConfig(name string) *TConfig {
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"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"` // deprecated in v4.1, moved to env
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` // public request headers
Environs map[string]string `json:"environs,omitempty" yaml:"environs,omitempty"` // environment variables
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` // global variables
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
ParametersSetting *TParamsConfig `json:"parameters_setting,omitempty" yaml:"parameters_setting,omitempty"`
ThinkTimeSetting *ThinkTimeConfig `json:"think_time,omitempty" yaml:"think_time,omitempty"`

View File

@@ -295,12 +295,44 @@ func LoadFile(path string, structObj interface{}) (err error) {
err = decoder.Decode(structObj)
case ".yaml", ".yml":
err = yaml.Unmarshal(file, structObj)
case ".env":
err = parseEnvContent(file, structObj)
default:
err = ErrUnsupportedFileExt
}
return err
}
func parseEnvContent(file []byte, obj interface{}) error {
envMap := obj.(map[string]string)
lines := strings.Split(string(file), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
// empty line or comment line
continue
}
var kv []string
if strings.Contains(line, "=") {
kv = strings.SplitN(line, "=", 2)
} else if strings.Contains(line, ":") {
kv = strings.SplitN(line, ":", 2)
}
if len(kv) != 2 {
return errors.New(".env format error")
}
key := strings.TrimSpace(kv[0])
value := strings.TrimSpace(kv[1])
envMap[key] = value
// set env
log.Info().Str("key", key).Msg("set env")
os.Setenv(key, value)
}
return nil
}
func loadFromCSV(path string) []map[string]interface{} {
log.Info().Str("path", path).Msg("load csv file")
file, err := readFile(path)

View File

@@ -104,7 +104,7 @@ func CreateScaffold(projectName string, pluginType PluginType, force bool) error
}
projectInfo := &ProjectInfo{
ProjectName: projectName,
ProjectName: filepath.Base(projectName),
ProjectPath: projectPath,
CreateTime: time.Now(),
Version: version.VERSION,

View File

@@ -1,2 +1,3 @@
base_url=https://postman-echo.com
USERNAME=debugtalk
PASSWORD=123456

View File

@@ -1,4 +1,3 @@
.env
reports/
*.so
.vscode/

View File

@@ -17,7 +17,7 @@
{
"name": "get with params",
"variables": {
"foo1": "bar11",
"foo1": "${ENV(USERNAME)}",
"foo2": "bar21",
"sum_v": "${sum_two_int(1, 2)}"
},
@@ -46,7 +46,7 @@
{
"eq": [
"body.args.foo1",
"bar11"
"debugtalk"
]
},
{

View File

@@ -5,7 +5,6 @@ config:
foo2: config_bar2
expect_foo1: config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
export: ["foo3"]
@@ -13,12 +12,12 @@ teststeps:
-
name: get with params
variables:
foo1: bar11
foo1: ${ENV(USERNAME)}
foo2: bar21
sum_v: "${sum_two_int(1, 2)}"
request:
method: GET
url: /get
url: $base_url/get
params:
foo1: $foo1
foo2: $foo2
@@ -29,7 +28,7 @@ teststeps:
foo3: "body.args.foo2"
validate:
- eq: ["status_code", 200]
- eq: ["body.args.foo1", "bar11"]
- eq: ["body.args.foo1", "debugtalk"]
- eq: ["body.args.sum_v", "3"]
- eq: ["body.args.foo2", "bar21"]
-
@@ -39,7 +38,7 @@ teststeps:
foo3: "bar32"
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "text/plain"
@@ -53,7 +52,7 @@ teststeps:
foo2: bar23
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "application/x-www-form-urlencoded"

View File

@@ -287,6 +287,25 @@ func (r *testCaseRunner) parseConfig() error {
}
r.parsedConfig.BaseURL = convertString(parsedBaseURL)
// merge config environment variables with base_url
// priority: env base_url > base_url
if cfg.Environs != nil {
r.parsedConfig.Environs = cfg.Environs
} else {
r.parsedConfig.Environs = make(map[string]string)
}
if value, ok := r.parsedConfig.Environs["base_url"]; !ok || value == "" {
if r.parsedConfig.BaseURL != "" {
r.parsedConfig.Environs["base_url"] = r.parsedConfig.BaseURL
}
}
// merge config variables with environment variables
// priority: env > config variables
for k, v := range r.parsedConfig.Environs {
r.parsedConfig.Variables[k] = v
}
// ensure correction of think time config
r.parsedConfig.ThinkTimeSetting.checkThinkTime()

View File

@@ -145,7 +145,8 @@ func (r *requestBuilder) prepareUrlParams(stepVariables map[string]interface{})
log.Error().Err(err).Msg("parse request url failed")
return err
}
rawUrl := buildURL(r.config.BaseURL, convertString(requestUrl))
baseURL := stepVariables["base_url"].(string)
rawUrl := buildURL(baseURL, convertString(requestUrl))
// prepare request params
var queryParams url.Values

View File

@@ -80,6 +80,25 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
return nil, errors.Wrap(err, "failed to get project root dir")
}
// load .env file
dotEnvPath := filepath.Join(projectRootDir, ".env")
if builtin.IsFilePathExists(dotEnvPath) {
envVars := make(map[string]string)
err = builtin.LoadFile(dotEnvPath, envVars)
if err != nil {
return nil, errors.Wrap(err, "failed to load .env file")
}
// override testcase config env with variables loaded from .env file
// priority: .env file > testcase config env
if testCase.Config.Environs == nil {
testCase.Config.Environs = make(map[string]string)
}
for key, value := range envVars {
testCase.Config.Environs[key] = value
}
}
for _, step := range tc.TestSteps {
if step.API != nil {
apiPath, ok := step.API.(string)