|
| 1 | +package formatters_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/xml" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cucumber/godog" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_JUnitFormatter_StopOnFirstFailure(t *testing.T) { |
| 13 | + featureFile := "formatter-tests/features/stop_on_first_failure.feature" |
| 14 | + |
| 15 | + // First, verify the normal output (without StopOnFirstFailure) |
| 16 | + var normalBuf bytes.Buffer |
| 17 | + normalOpts := godog.Options{ |
| 18 | + Format: "junit", |
| 19 | + Paths: []string{featureFile}, |
| 20 | + Output: &normalBuf, |
| 21 | + Strict: true, |
| 22 | + } |
| 23 | + |
| 24 | + normalSuite := godog.TestSuite{ |
| 25 | + Name: "Normal Run", |
| 26 | + ScenarioInitializer: func(sc *godog.ScenarioContext) { |
| 27 | + setupStopOnFailureSteps(sc) |
| 28 | + }, |
| 29 | + Options: &normalOpts, |
| 30 | + } |
| 31 | + if status := normalSuite.Run(); status != 1 { |
| 32 | + t.Fatalf("Expected suite to have status 1, but got %d", status) |
| 33 | + } |
| 34 | + |
| 35 | + // Parse the XML output |
| 36 | + var normalResult JunitPackageSuite |
| 37 | + err := xml.Unmarshal(normalBuf.Bytes(), &normalResult) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Failed to parse XML output: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Now run with StopOnFirstFailure |
| 43 | + var stopBuf bytes.Buffer |
| 44 | + stopOpts := godog.Options{ |
| 45 | + Format: "junit", |
| 46 | + Paths: []string{featureFile}, |
| 47 | + Output: &stopBuf, |
| 48 | + Strict: true, |
| 49 | + StopOnFailure: true, |
| 50 | + } |
| 51 | + |
| 52 | + stopSuite := godog.TestSuite{ |
| 53 | + Name: "Stop On First Failure", |
| 54 | + ScenarioInitializer: func(sc *godog.ScenarioContext) { |
| 55 | + setupStopOnFailureSteps(sc) |
| 56 | + }, |
| 57 | + Options: &stopOpts, |
| 58 | + } |
| 59 | + if status := stopSuite.Run(); status != 1 { |
| 60 | + t.Fatalf("Expected suite to have status 1, but got %d", status) |
| 61 | + } |
| 62 | + |
| 63 | + // Parse the XML output |
| 64 | + var stopResult JunitPackageSuite |
| 65 | + err = xml.Unmarshal(stopBuf.Bytes(), &stopResult) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("Failed to parse XML output: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Verify the second test case is marked as skipped when StopOnFirstFailure is enabled |
| 71 | + if len(stopResult.TestSuites) == 0 || len(stopResult.TestSuites[0].TestCases) < 2 { |
| 72 | + t.Fatal("Expected at least 2 test cases in the results") |
| 73 | + } |
| 74 | + |
| 75 | + // In a normal run, second test case should not be skipped |
| 76 | + if normalResult.TestSuites[0].TestCases[1].Status == "skipped" { |
| 77 | + t.Errorf("In normal run, second test case should not be skipped") |
| 78 | + } |
| 79 | + |
| 80 | + // In stop on failure run, second test case should be skipped |
| 81 | + if stopResult.TestSuites[0].TestCases[1].Status != "skipped" { |
| 82 | + t.Errorf("In stop on failure run, second test case should be skipped, but got %s", |
| 83 | + stopResult.TestSuites[0].TestCases[1].Status) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// setupStopOnFailureSteps registers the step definitions for the stop-on-failure test |
| 88 | +func setupStopOnFailureSteps(sc *godog.ScenarioContext) { |
| 89 | + sc.Step(`^a passing step$`, func() error { |
| 90 | + return nil |
| 91 | + }) |
| 92 | + sc.Step(`^a failing step$`, func() error { |
| 93 | + return fmt.Errorf("step failed") |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +// JunitPackageSuite represents the JUnit XML structure for test suites |
| 98 | +type JunitPackageSuite struct { |
| 99 | + XMLName xml.Name `xml:"testsuites"` |
| 100 | + Name string `xml:"name,attr"` |
| 101 | + Tests int `xml:"tests,attr"` |
| 102 | + Skipped int `xml:"skipped,attr"` |
| 103 | + Failures int `xml:"failures,attr"` |
| 104 | + Errors int `xml:"errors,attr"` |
| 105 | + Time string `xml:"time,attr"` |
| 106 | + TestSuites []*JunitTestSuite `xml:"testsuite"` |
| 107 | +} |
| 108 | + |
| 109 | +type JunitTestSuite struct { |
| 110 | + XMLName xml.Name `xml:"testsuite"` |
| 111 | + Name string `xml:"name,attr"` |
| 112 | + Tests int `xml:"tests,attr"` |
| 113 | + Skipped int `xml:"skipped,attr"` |
| 114 | + Failures int `xml:"failures,attr"` |
| 115 | + Errors int `xml:"errors,attr"` |
| 116 | + Time string `xml:"time,attr"` |
| 117 | + TestCases []*JunitTestCase `xml:"testcase"` |
| 118 | +} |
| 119 | + |
| 120 | +type JunitTestCase struct { |
| 121 | + XMLName xml.Name `xml:"testcase"` |
| 122 | + Name string `xml:"name,attr"` |
| 123 | + Status string `xml:"status,attr"` |
| 124 | + Time string `xml:"time,attr"` |
| 125 | + Failure *JunitFailure `xml:"failure,omitempty"` |
| 126 | + Error []*JunitError `xml:"error,omitempty"` |
| 127 | +} |
| 128 | + |
| 129 | +type JunitFailure struct { |
| 130 | + Message string `xml:"message,attr"` |
| 131 | + Type string `xml:"type,attr,omitempty"` |
| 132 | +} |
| 133 | + |
| 134 | +type JunitError struct { |
| 135 | + XMLName xml.Name `xml:"error,omitempty"` |
| 136 | + Message string `xml:"message,attr"` |
| 137 | + Type string `xml:"type,attr"` |
| 138 | +} |
0 commit comments