This repository has been archived by the owner on Feb 13, 2025. It is now read-only.
forked from scenarigo/scenarigo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an option to output test summary at last (scenarigo#395)
* feat: test summary * Make show summary configurable * enable color if enabledColor * change `NoSummary` to `Summary`. * implement `PrintSummary` as `reporter`'s method * delete unnecessary line break * Add nil check for `cfg` * fix unit tests for `NewRunner` * update code snippet generated by `scenarigo config init` in README * change `testSummary#add`'s argument(`r reporter.Reporter` to `testResultString string`) for the testability * add unit tests for `testSummary#add` * add unit tests for `testSummary#String`, `testSummary#failedFiles` * add unit test for `run` when summary is enabled * move `summary.go`, `summary_test.go` to `reporter` package from `scenarigo` package * move logic to `reporter` package from `scenarigo` package * rename `testSummary#add` to `testSummary#append` * rename `reporter/summary.go` to `reporter/test_summary.go`, `reporter/summary_test.go` to `reporter/test_summary_test.go` * show failed files with failColor * delete unnecessary line break * delete `enabledColor` field from `testSummary` * delete line break * If `testSummary == nil` do nothing in `testSummary#append`, change interface of `testSummary#append` * keep only failed file names
- Loading branch information
Showing
10 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
schemaVersion: config/v1 | ||
|
||
scenarios: | ||
- ./scenarios/fail.yaml | ||
- ./scenarios/pass.yaml | ||
pluginDirectory: ./ # Specify the root directory of plugins. | ||
|
||
output: | ||
verbose: false # Enable verbose output. | ||
# colored: false # Enable colored output with ANSI color escape codes. It is enabled by default but disabled when a NO_COLOR environment variable is set (regardless of its value). | ||
summary: true # Enable summary output. | ||
# report: | ||
# json: | ||
# filename: ./report.json # Specify a filename for test report output in JSON. | ||
# junit: | ||
# filename: ./junit.xml # Specify a filename for test report output in JUnit XML format. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package reporter | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
type testSummary struct { | ||
mu sync.Mutex | ||
passedCount int | ||
failed []string | ||
skippedCount int | ||
} | ||
|
||
func newTestSummary() *testSummary { | ||
return &testSummary{ | ||
mu: sync.Mutex{}, | ||
passedCount: 0, | ||
failed: []string{}, | ||
skippedCount: 0, | ||
} | ||
} | ||
|
||
func (s *testSummary) append(testFileRelPath string, r Reporter) { | ||
if s == nil { | ||
return | ||
} | ||
testResultString := TestResultString(r) | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
switch testResultString { | ||
case TestResultPassed.String(): | ||
s.passedCount++ | ||
case TestResultFailed.String(): | ||
s.failed = append(s.failed, testFileRelPath) | ||
case TestResultSkipped.String(): | ||
s.skippedCount++ | ||
default: // Do nothing | ||
} | ||
} | ||
|
||
// String converts testSummary to the string like below. | ||
// 11 tests run: 9 passed, 2 failed, 0 skipped | ||
// | ||
// Failed tests: | ||
// - scenarios/scenario1.yaml | ||
// - scenarios/scenario2.yaml | ||
func (s *testSummary) String(noColor bool) string { | ||
totalText := fmt.Sprintf("%d tests run", s.passedCount+len(s.failed)+s.skippedCount) | ||
passedText := s.passColor(noColor).Sprintf("%d passed", s.passedCount) | ||
failedText := s.failColor(noColor).Sprintf("%d failed", len(s.failed)) | ||
skippedText := s.skipColor(noColor).Sprintf("%d skipped", s.skippedCount) | ||
failedFiles := s.failColor(noColor).Sprintf(s.failedFiles()) | ||
return fmt.Sprintf( | ||
"\n%s: %s, %s, %s\n\n%s", | ||
totalText, passedText, failedText, skippedText, failedFiles, | ||
) | ||
} | ||
|
||
func (s *testSummary) failedFiles() string { | ||
if len(s.failed) == 0 { | ||
return "" | ||
} | ||
|
||
result := "" | ||
|
||
for _, f := range s.failed { | ||
if result == "" { | ||
result = "Failed tests:\n" | ||
} | ||
result += fmt.Sprintf("\t- %s\n", f) | ||
} | ||
result += "\n" | ||
|
||
return result | ||
} | ||
|
||
func (s *testSummary) passColor(noColor bool) *color.Color { | ||
if noColor { | ||
return color.New() | ||
} | ||
return color.New(color.FgGreen) | ||
} | ||
|
||
func (s *testSummary) failColor(noColor bool) *color.Color { | ||
if noColor { | ||
return color.New() | ||
} | ||
return color.New(color.FgHiRed) | ||
} | ||
|
||
func (s *testSummary) skipColor(noColor bool) *color.Color { | ||
if noColor { | ||
return color.New() | ||
} | ||
return color.New(color.FgYellow) | ||
} |
Oops, something went wrong.