Skip to content

Commit 969896e

Browse files
committed
Add min-pass-ratio to run cmd
1 parent 6e5d37c commit 969896e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var RunTestsCmd = &cobra.Command{
2626
threshold, _ := cmd.Flags().GetFloat64("threshold")
2727
skipTests, _ := cmd.Flags().GetStringSlice("skip-tests")
2828
printFailedTests, _ := cmd.Flags().GetBool("print-failed-tests")
29+
minPassRatio, _ := cmd.Flags().GetFloat64("min-pass-ratio")
2930

3031
// Check if project dependencies are correctly set up
3132
if err := checkDependencies(projectPath); err != nil {
@@ -61,14 +62,17 @@ var RunTestsCmd = &cobra.Command{
6162
passedTests := reports.FilterPassedTests(testResults, threshold)
6263
failedTests := reports.FilterFailedTests(testResults, threshold)
6364
skippedTests := reports.FilterSkippedTests(testResults)
65+
flakyTests := reports.FilterFlakyTests(testResults, minPassRatio, threshold)
6466

67+
// Print all failed tests including flaky tests
6568
if len(failedTests) > 0 && printFailedTests {
69+
fmt.Printf("MinPassRatio threshold for flaky tests: %.2f\n", minPassRatio)
6670
fmt.Printf("PassRatio threshold for flaky tests: %.2f\n", threshold)
6771
fmt.Printf("%d failed tests:\n", len(failedTests))
6872
reports.PrintTests(failedTests, os.Stdout)
6973
}
7074

71-
fmt.Printf("Summary: %d passed, %d skipped, %d failed\n", len(passedTests), len(skippedTests), len(failedTests))
75+
fmt.Printf("Summary: %d passed, %d skipped, %d failed, %d flaky\n", len(passedTests), len(skippedTests), len(failedTests), len(flakyTests))
7276

7377
// Save the test results in JSON format
7478
if outputPath != "" && len(testResults) > 0 {
@@ -82,8 +86,8 @@ var RunTestsCmd = &cobra.Command{
8286
fmt.Printf("All test results saved to %s\n", outputPath)
8387
}
8488

85-
if len(failedTests) > 0 {
86-
// Fail if any tests failed
89+
if len(flakyTests) > 0 {
90+
// Exit with error code if there are flaky tests
8791
os.Exit(1)
8892
} else if len(testResults) == 0 {
8993
fmt.Printf("No tests were run for the specified packages.\n")
@@ -103,6 +107,7 @@ func init() {
103107
RunTestsCmd.Flags().Float64("threshold", 0.8, "Threshold for considering a test as flaky")
104108
RunTestsCmd.Flags().StringSlice("skip-tests", nil, "Comma-separated list of test names to skip from running")
105109
RunTestsCmd.Flags().Bool("print-failed-tests", true, "Print failed test results to the console")
110+
RunTestsCmd.Flags().Float64("min-pass-ratio", 0.001, "Minimum pass ratio for considering a test as flaky. Used to distinguish between tests that are truly flaky (with inconsistent results) and those that are consistently failing.")
106111
}
107112

108113
func checkDependencies(projectPath string) error {

tools/flakeguard/reports/reports.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ func FilterFailedTests(results []TestResult, threshold float64) []TestResult {
3636
return failedTests
3737
}
3838

39+
// FilterFlakyTests returns a slice of TestResult where the pass ratio is between the min pass ratio and the threshold.
40+
func FilterFlakyTests(testResults []TestResult, minPassRatio, threshold float64) []TestResult {
41+
var flakyTests []TestResult
42+
for _, test := range testResults {
43+
if test.PassRatio >= minPassRatio && test.PassRatio < threshold && !test.Skipped {
44+
flakyTests = append(flakyTests, test)
45+
}
46+
}
47+
return flakyTests
48+
}
49+
3950
// FilterPassedTests returns a slice of TestResult where the tests passed and were not skipped.
4051
func FilterPassedTests(results []TestResult, threshold float64) []TestResult {
4152
var passedTests []TestResult

0 commit comments

Comments
 (0)