| 1 | package reporting_fixture_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | . "github.com/onsi/ginkgo/v2" |
| 9 | "github.com/onsi/ginkgo/v2/types" |
| 10 | . "github.com/onsi/gomega" |
| 11 | ) |
| 12 | |
| 13 | func TestReportingFixture(t *testing.T) { |
| 14 | RegisterFailHandler(Fail) |
| 15 | RunSpecs(t, "ReportingFixture Suite") |
| 16 | } |
| 17 | |
| 18 | var beforeEachReport, afterEachReport *os.File |
| 19 | |
| 20 | var _ = ReportBeforeSuite(func(report Report) { |
| 21 | f, err := os.Create(fmt.Sprintf("report-before-suite-%d.out", GinkgoParallelProcess())) |
| 22 | Ω(err).ShouldNot(HaveOccurred()) |
| 23 | |
| 24 | fmt.Fprintf(f, "%s - %d\n", report.SuiteDescription, report.SuiteConfig.RandomSeed) |
| 25 | fmt.Fprintf(f, "%d of %d", report.PreRunStats.SpecsThatWillRun, report.PreRunStats.TotalSpecs) |
| 26 | |
| 27 | f.Close() |
| 28 | }) |
| 29 | |
| 30 | var _ = BeforeSuite(func() { |
| 31 | var err error |
| 32 | beforeEachReport, err = os.Create("report-before-each.out") |
| 33 | Ω(err).ShouldNot(HaveOccurred()) |
| 34 | DeferCleanup(beforeEachReport.Close) |
| 35 | |
| 36 | afterEachReport, err = os.Create("report-after-each.out") |
| 37 | Ω(err).ShouldNot(HaveOccurred()) |
| 38 | DeferCleanup(afterEachReport.Close) |
| 39 | }) |
| 40 | |
| 41 | var _ = ReportBeforeEach(func(report SpecReport) { |
| 42 | fmt.Fprintf(beforeEachReport, "%s - %s\n", report.LeafNodeText, report.State) |
| 43 | }) |
| 44 | |
| 45 | var _ = ReportAfterEach(func(report SpecReport) { |
| 46 | fmt.Fprintf(afterEachReport, "%s - %s\n", report.LeafNodeText, report.State) |
| 47 | }) |
| 48 | |
| 49 | var _ = ReportAfterSuite("my report", func(report Report) { |
| 50 | f, err := os.Create("report-after-suite.out") |
| 51 | Ω(err).ShouldNot(HaveOccurred()) |
| 52 | |
| 53 | fmt.Fprintf(f, "%s - %d\n", report.SuiteDescription, report.SuiteConfig.RandomSeed) |
| 54 | for _, specReport := range report.SpecReports { |
| 55 | if specReport.LeafNodeType.Is(types.NodeTypesForSuiteLevelNodes) || specReport.LeafNodeType.Is(types.NodeTypeCleanupAfterSuite) { |
| 56 | fmt.Fprintf(f, "%d: [%s] - %s\n", specReport.ParallelProcess, specReport.LeafNodeType, specReport.State) |
| 57 | } else { |
| 58 | fmt.Fprintf(f, "%s - %s\n", specReport.LeafNodeText, specReport.State) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | f.Close() |
| 63 | |
| 64 | Fail("fail!") |
| 65 | }) |