From 4827ebf284ba41aad15ccefa440d99f1da26316a Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 14 Nov 2021 21:40:05 +0800 Subject: [PATCH] fix: run boomer test --- boomer.go | 5 +++-- runner.go | 43 +++++++++++++++++++++++-------------------- step_test.go | 5 +++-- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/boomer.go b/boomer.go index d3e2532..fdeff43 100644 --- a/boomer.go +++ b/boomer.go @@ -43,11 +43,12 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task { Weight: testcase.Config.Weight, Fn: func() { config := &testcase.Config + extractedVariables := make(map[string]interface{}) for _, step := range testcase.TestSteps { + var err error start := time.Now() - stepData, err := runner.runStep(step, config) + stepData, err := runner.runStep(step, config, extractedVariables) elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond) - if err == nil { boomer.RecordSuccess(step.Type(), step.Name(), elapsed, stepData.ResponseLength) } else { diff --git a/runner.go b/runner.go index 8acc56a..c630c5d 100644 --- a/runner.go +++ b/runner.go @@ -91,36 +91,33 @@ func (r *Runner) runCase(testcase *TestCase) error { extractedVariables := make(map[string]interface{}) for _, step := range testcase.TestSteps { - // override variables - // step variables > extracted variables from previous steps - stepVariables := mergeVariables(step.ToStruct().Variables, extractedVariables) - // step variables > testcase config variables - stepVariables = mergeVariables(stepVariables, config.Variables) - - // parse step variables - parsedVariables, err := parseVariables(stepVariables) + _, err := r.runStep(step, config, extractedVariables) if err != nil { - log.Error().Interface("variables", config.Variables).Err(err).Msg("parse step variables failed") return err } - step.ToStruct().Variables = parsedVariables - - stepData, err := r.runStep(step, config) - if err != nil { - return err - } - // update extracted variables - for k, v := range stepData.ExportVars { - extractedVariables[k] = v - } } log.Info().Str("testcase", config.Name).Msg("run testcase end") return nil } -func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err error) { +func (r *Runner) runStep(step IStep, config *TConfig, extractedVariables map[string]interface{}) (stepData *StepData, err error) { log.Info().Str("step", step.Name()).Msg("run step start") + + // override variables + // step variables > extracted variables from previous steps + stepVariables := mergeVariables(step.ToStruct().Variables, extractedVariables) + // step variables > testcase config variables + stepVariables = mergeVariables(stepVariables, config.Variables) + + // parse step variables + parsedVariables, err := parseVariables(stepVariables) + if err != nil { + log.Error().Interface("variables", config.Variables).Err(err).Msg("parse step variables failed") + return + } + step.ToStruct().Variables = parsedVariables + if tc, ok := step.(*testcaseWithOptionalArgs); ok { // run referenced testcase log.Info().Str("testcase", tc.step.Name).Msg("run referenced testcase") @@ -139,6 +136,12 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e return } } + + // update extracted variables + for k, v := range stepData.ExportVars { + extractedVariables[k] = v + } + log.Info(). Str("step", step.Name()). Bool("success", stepData.Success). diff --git a/step_test.go b/step_test.go index 1f2e56e..2aea0c2 100644 --- a/step_test.go +++ b/step_test.go @@ -77,11 +77,12 @@ func TestRunRequestRun(t *testing.T) { config := &TConfig{ BaseURL: "https://postman-echo.com", } + extractedVariables := make(map[string]interface{}) runner := NewRunner(t).SetDebug(true) - if _, err := runner.runStep(stepGET, config); err != nil { + if _, err := runner.runStep(stepGET, config, extractedVariables); err != nil { t.Fatalf("tStep.Run() error: %s", err) } - if _, err := runner.runStep(stepPOSTData, config); err != nil { + if _, err := runner.runStep(stepPOSTData, config, extractedVariables); err != nil { t.Fatalf("tStepPOSTData.Run() error: %s", err) } }