From 5090be8a01ae5e18c195f6f4d60efa9dbdf447a3 Mon Sep 17 00:00:00 2001 From: Yvonnick Esnault Date: Mon, 12 Sep 2022 21:14:32 +0200 Subject: [PATCH] feat: improve output with range (#588) * feat: improve output with range Signed-off-by: Yvonnick Esnault --- process_testcase.go | 20 ++++++++++++-------- process_testsuite.go | 15 ++++++++++----- tests/verbose_output.yml | 6 +++--- types.go | 1 + 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/process_testcase.go b/process_testcase.go index 5c2a9e7a..d109ea86 100644 --- a/process_testcase.go +++ b/process_testcase.go @@ -268,6 +268,7 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe tsResult.Number = stepNumber tsResult.RangedIndex = rangedIndex + tsResult.RangedEnable = ranged.Enabled tsResult.InputVars = vars tc.testSteps = append(tc.testSteps, step) @@ -299,7 +300,7 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe } printStepName := v.Verbose >= 1 && !fromUserExecutor - v.setTestStepName(tsResult, e, step, &ranged, &rangedData, printStepName) + v.setTestStepName(tsResult, e, step, &ranged, &rangedData, rangedIndex, printStepName) tsResult.Start = time.Now() @@ -332,13 +333,13 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe if isRequired { failure := newFailure(ctx, *tc, stepNumber, rangedIndex, "", fmt.Errorf("At least one required assertion failed, skipping remaining steps")) tsResult.appendFailure(*failure) - v.printTestStepResult(tc, tsResult, tsIn, stepNumber, true) + v.printTestStepResult(tc, tsResult, tsIn, ranged, stepNumber, true) return } - v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false) + v.printTestStepResult(tc, tsResult, tsIn, ranged, stepNumber, false) continue } - v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false) + v.printTestStepResult(tc, tsResult, tsIn, ranged, stepNumber, false) allVars := tc.Vars.Clone() allVars.AddAll(tc.computedVars.Clone()) @@ -358,7 +359,7 @@ func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepRe } // Set test step name (defaults to executor name, excepted if it got a "name" attribute. in range, also print key) -func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestStep, ranged *Range, rangedData *RangeData, print bool) { +func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestStep, ranged *Range, rangedData *RangeData, rangedIndex int, print bool) { name := e.Name() if value, ok := step["name"]; ok { switch value := value.(type) { @@ -367,22 +368,25 @@ func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestS } } if ranged.Enabled { + if rangedIndex == 0 { + v.Print("\n") + } name = fmt.Sprintf("%s (range=%s)", name, rangedData.Key) } ts.Name = name - if print { + if print || ranged.Enabled { v.Print(" \t\t• %s", ts.Name) } } // Print a single step result (if verbosity is enabled) -func (v *Venom) printTestStepResult(tc *TestCase, ts *TestStepResult, tsIn *TestStepResult, stepNumber int, mustAssertionFailed bool) { +func (v *Venom) printTestStepResult(tc *TestCase, ts *TestStepResult, tsIn *TestStepResult, ranged Range, stepNumber int, mustAssertionFailed bool) { fromUserExecutor := tsIn != nil if fromUserExecutor { tsIn.appendFailure(ts.Errors...) } - if v.Verbose >= 1 { + if ranged.Enabled || v.Verbose >= 1 { if !fromUserExecutor { //Else print step status if len(ts.Errors) > 0 { v.Println(" %s", Red(StatusFail)) diff --git a/process_testsuite.go b/process_testsuite.go index 48108819..c2da5dec 100644 --- a/process_testsuite.go +++ b/process_testsuite.go @@ -104,10 +104,8 @@ func (v *Venom) runTestCases(ctx context.Context, ts *TestSuite) { tc := &ts.TestCases[i] tc.IsEvaluated = true v.Print(" \t• %s", tc.Name) - if verboseReport { - v.Print("\n") - } var hasFailure bool + var hasRanged bool var hasSkipped = len(tc.Skipped) > 0 if !hasSkipped { start := time.Now() @@ -120,11 +118,18 @@ func (v *Venom) runTestCases(ctx context.Context, ts *TestSuite) { } for _, testStepResult := range tc.TestStepResults { + if testStepResult.RangedEnable { + hasRanged = true + } if testStepResult.Status == StatusFail { hasFailure = true } } + if verboseReport || hasRanged { + v.Print("\n") + } + if hasFailure { tc.Status = StatusFail } else if tc.Status != StatusSkip { @@ -133,7 +138,7 @@ func (v *Venom) runTestCases(ctx context.Context, ts *TestSuite) { // Verbose mode already reported tests status, so just print them when non-verbose indent := "" - if verboseReport { + if hasRanged || verboseReport { indent = "\t " } else { if hasFailure { @@ -155,7 +160,7 @@ func (v *Venom) runTestCases(ctx context.Context, ts *TestSuite) { } // Verbose mode already reported failures, so just print them when non-verbose - if !verboseReport && hasFailure { + if !hasRanged && !verboseReport && hasFailure { for _, testStepResult := range tc.TestStepResults { for _, f := range testStepResult.Errors { v.Println("%s", Yellow(f.Value)) diff --git a/tests/verbose_output.yml b/tests/verbose_output.yml index d84778b5..e8595601 100644 --- a/tests/verbose_output.yml +++ b/tests/verbose_output.yml @@ -25,9 +25,9 @@ testcases: - result.systemout {{.value.op}}ContainSubstring 'step1 PASS' - result.systemout {{.value.op}}ContainSubstring 'step2 FAIL' # ranged steps - - result.systemout {{.value.op}}ContainSubstring 'exec (range=0) PASS' - - result.systemout {{.value.op}}ContainSubstring 'exec (range=1) FAIL' - - result.systemout {{.value.op}}ContainSubstring 'exec (range=2) PASS' + - result.systemout ShouldContainSubstring 'exec (range=0) PASS' + - result.systemout ShouldContainSubstring 'exec (range=1) FAIL' + - result.systemout ShouldContainSubstring 'exec (range=2) PASS' # must assertions - result.systemout {{.value.op}}ContainSubstring 'must1 FAIL' - result.systemout ShouldContainSubstring 'At least one required assertion failed, skipping remaining steps' diff --git a/types.go b/types.go index 023554a7..59c231d7 100644 --- a/types.go +++ b/types.go @@ -180,6 +180,7 @@ type TestStepResult struct { Interpolated interface{} `json:"interpolated" yaml:"interpolated"` Number int `json:"number" yaml:"number"` RangedIndex int `json:"rangedIndex" yaml:"rangedIndex"` + RangedEnable bool `json:"rangedEnable" yaml:"rangedEnable"` InputVars map[string]string `json:"inputVars" yaml:"-"` ComputedVars H `json:"computedVars" yaml:"-"` ComputedInfo []string `json:"computedInfos" yaml:"-"`