change: replace logs

This commit is contained in:
debugtalk
2021-10-17 14:51:44 +08:00
parent 2fd45a5d63
commit b77f935339
5 changed files with 35 additions and 16 deletions

View File

@@ -70,7 +70,7 @@ func loadFromJSON(path string) (*TCase, error) {
log.Errorf("convert absolute path error: %v, path: %v", err, path)
return nil, err
}
log.Infof("load testcase from json path: %s", path)
log.WithField("path", path).Info("load json testcase")
file, err := ioutil.ReadFile(path)
if err != nil {

View File

@@ -120,7 +120,11 @@ func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) {
}
func (h *HAR) prepareTestStep(entry *Entry) (*hrp.TStep, error) {
log.Infof("[prepareTestStep] %v %v", entry.Request.Method, entry.Request.URL)
log.WithFields(log.Fields{
"method": entry.Request.Method,
"url": entry.Request.URL,
}).Info("convert teststep")
tStep := &TStep{
TStep: hrp.TStep{
Request: &hrp.TRequest{},

View File

@@ -291,10 +291,7 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
}
log.Infof("[callFunction] func: %v, input arguments: %v", funcName, arguments)
resultValues := funcValue.Call(argumentsValue)
log.Infof("[callFunction] output values: %v", resultValues)
if len(resultValues) > 1 {
// function should return at most one value
err := fmt.Errorf("function %s should return at most one value", funcName)
@@ -310,7 +307,11 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
// return one value
// convert reflect.Value to interface{}
result := resultValues[0].Interface()
log.Infof("[callFunction] output result: %+v(%T)", result, result)
log.WithFields(log.Fields{
"funcName": funcName,
"arguments": arguments,
"output": result,
}).Info("call function")
return result, nil
}

View File

@@ -77,8 +77,8 @@ func (v *ResponseObject) Extract(extractors map[string]string) map[string]interf
extractMapping := make(map[string]interface{})
for key, value := range extractors {
extractedValue := v.searchJmespath(value)
log.Infof("[extract] %s => %v", value, extractedValue)
log.Infof("[setVariable] %s = %v", key, extractedValue)
log.WithField("value", extractedValue).Infof("extract value from %s", value)
log.WithField("value", extractedValue).Infof("set variable %s", key)
extractMapping[key] = extractedValue
}
@@ -112,7 +112,12 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[
// do assertion
result := assertFunc(v.t, expectValue, checkValue)
log.Infof("[assert] %s <%s> %v => %v", checkItem, assertMethod, expectValue, result)
log.WithFields(log.Fields{
"assertMethod": assertMethod,
"expectValue": expectValue,
"checkValue": checkValue,
"result": result,
}).Infof("validate %s", checkItem)
if !result {
v.t.Fail()
}

View File

@@ -9,6 +9,11 @@ import (
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{})
}
// run API test with default configs
func Run(t *testing.T, testcases ...ITestCase) error {
return NewRunner().WithTestingT(t).SetDebug(true).Run(testcases...)
@@ -29,19 +34,19 @@ type Runner struct {
}
func (r *Runner) WithTestingT(t *testing.T) *Runner {
log.Infof("[init] WithTestingT: %v", t)
log.Info("[init] WithTestingT")
r.t = t
return r
}
func (r *Runner) SetDebug(debug bool) *Runner {
log.Infof("[init] SetDebug: %v", debug)
log.WithField("debug", debug).Info("[init] SetDebug")
r.debug = debug
return r
}
func (r *Runner) SetProxyUrl(proxyUrl string) *Runner {
log.Infof("[init] SetProxyUrl: %s", proxyUrl)
log.WithField("proxyUrl", proxyUrl).Info("[init] SetProxyUrl")
r.client.SetProxyUrl(proxyUrl)
return r
}
@@ -67,7 +72,7 @@ func (r *Runner) runCase(testcase *TestCase) error {
return err
}
log.Infof("Start to run testcase: %v", config.Name)
log.WithField("testcase", config.Name).Info("run testcase start")
extractedVariables := make(map[string]interface{})
@@ -96,11 +101,12 @@ func (r *Runner) runCase(testcase *TestCase) error {
}
}
log.WithField("testcase", config.Name).Info("run testcase end")
return nil
}
func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err error) {
log.Infof("run step [%v] begin >>>>>>", step.Name())
log.WithField("step", step.Name()).Info("run step start")
if tc, ok := step.(*testcaseWithOptionalArgs); ok {
// run referenced testcase
log.Infof("run referenced testcase: %v", tc.step.Name)
@@ -117,8 +123,11 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e
return
}
}
log.Infof("run step [%v] end, success: %v, responseLength: %v <<<<<<\n",
step.Name(), stepData.Success, stepData.ResponseLength)
log.WithFields(log.Fields{
"step": step.Name(),
"success": stepData.Success,
"exportVars": stepData.ExportVars,
}).Info("run step end")
return
}