github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/internal_integration/fail_test.go (about)

     1  package internal_integration_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo"
     5  	. "github.com/onsi/gomega"
     6  
     7  	. "github.com/onsi/ginkgo/internal/test_helpers"
     8  	"github.com/onsi/ginkgo/types"
     9  )
    10  
    11  var _ = Describe("handling test failures", func() {
    12  	Describe("when BeforeSuite fails", func() {
    13  		BeforeEach(func() {
    14  			success, _ := RunFixture("failed beforesuite", func() {
    15  				BeforeSuite(rt.T("before-suite", func() {
    16  					writer.Write([]byte("before-suite"))
    17  					F("fail", cl)
    18  				}))
    19  				It("A", rt.T("A"))
    20  				It("B", rt.T("B"))
    21  				AfterSuite(rt.T("after-suite"))
    22  			})
    23  			Ω(success).Should(BeFalse())
    24  		})
    25  
    26  		It("reports a suite failure", func() {
    27  			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(2), NSkipped(0)))
    28  		})
    29  
    30  		It("reports a failure for the BeforeSuite", func() {
    31  			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeBeforeSuite)).Should(HaveFailed("fail", cl, CapturedGinkgoWriterOutput("before-suite")))
    32  			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeAfterSuite)).Should(HavePassed())
    33  		})
    34  
    35  		It("does not run any of the Its", func() {
    36  			Ω(rt).ShouldNot(HaveRun("A"))
    37  			Ω(rt).ShouldNot(HaveRun("B"))
    38  		})
    39  
    40  		It("does run the AfterSuite", func() {
    41  			Ω(rt).Should(HaveTracked("before-suite", "after-suite"))
    42  		})
    43  	})
    44  
    45  	Describe("when BeforeSuite panics", func() {
    46  		BeforeEach(func() {
    47  			success, _ := RunFixture("panicked beforesuite", func() {
    48  				BeforeSuite(rt.T("before-suite", func() {
    49  					writer.Write([]byte("before-suite"))
    50  					panic("boom")
    51  				}))
    52  				It("A", rt.T("A"))
    53  				It("B", rt.T("B"))
    54  				AfterSuite(rt.T("after-suite"))
    55  			})
    56  			Ω(success).Should(BeFalse())
    57  		})
    58  
    59  		It("reports a suite failure", func() {
    60  			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(2), NSkipped(0)))
    61  		})
    62  
    63  		It("reports a failure for the BeforeSuite", func() {
    64  			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeBeforeSuite)).Should(HavePanicked("boom", CapturedGinkgoWriterOutput("before-suite")))
    65  		})
    66  
    67  		It("does not run any of the Its", func() {
    68  			Ω(rt).ShouldNot(HaveRun("A"))
    69  			Ω(rt).ShouldNot(HaveRun("B"))
    70  		})
    71  
    72  		It("does run the AfterSuite", func() {
    73  			Ω(rt).Should(HaveTracked("before-suite", "after-suite"))
    74  		})
    75  	})
    76  
    77  	Describe("when AfterSuite fails/panics", func() {
    78  		BeforeEach(func() {
    79  			success, _ := RunFixture("failed aftersuite", func() {
    80  				BeforeSuite(rt.T("before-suite"))
    81  				Describe("top-level", func() {
    82  					It("A", rt.T("A"))
    83  					It("B", rt.T("B"))
    84  				})
    85  				AfterSuite(rt.T("after-suite", func() {
    86  					writer.Write([]byte("after-suite"))
    87  					F("fail", cl)
    88  				}))
    89  			})
    90  			Ω(success).Should(BeFalse())
    91  		})
    92  
    93  		It("reports a suite failure", func() {
    94  			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(2), NPassed(2)))
    95  		})
    96  
    97  		It("runs and reports on all the tests and reports a failure for the AfterSuite", func() {
    98  			Ω(rt).Should(HaveTracked("before-suite", "A", "B", "after-suite"))
    99  			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeBeforeSuite)).Should(HavePassed())
   100  			Ω(reporter.Did.Find("A")).Should(HavePassed())
   101  			Ω(reporter.Did.Find("B")).Should(HavePassed())
   102  			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeAfterSuite)).Should(HaveFailed("fail", cl, CapturedGinkgoWriterOutput("after-suite")))
   103  		})
   104  	})
   105  
   106  	Describe("individual test falures", func() {
   107  		Describe("when an It fails", func() {
   108  			BeforeEach(func() {
   109  				success, _ := RunFixture("failed it", func() {
   110  					BeforeSuite(rt.T("before-suite"))
   111  					Describe("top-level", func() {
   112  						It("A", rt.T("A", func() {
   113  							writer.Write([]byte("running A"))
   114  						}))
   115  						It("B", rt.T("B", func() {
   116  							writer.Write([]byte("running B"))
   117  							F("fail", cl)
   118  						}))
   119  						It("C", rt.T("C"))
   120  					})
   121  					AfterEach(rt.T("after-each"))
   122  					AfterSuite(rt.T("after-suite"))
   123  				})
   124  				Ω(success).Should(BeFalse())
   125  			})
   126  
   127  			It("reports a suite failure", func() {
   128  				Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(3), NPassed(2), NFailed(1)))
   129  			})
   130  
   131  			It("runs other Its, the AfterEach, and the AfterSuite", func() {
   132  				Ω(rt).Should(HaveTracked("before-suite", "A", "after-each", "B", "after-each", "C", "after-each", "after-suite"))
   133  			})
   134  
   135  			It("reports the It's failure", func() {
   136  				Ω(reporter.Did.Find("A")).Should(HavePassed(CapturedGinkgoWriterOutput("running A")))
   137  				Ω(reporter.Did.Find("B")).Should(HaveFailed("fail", cl, CapturedGinkgoWriterOutput("running B")))
   138  				Ω(reporter.Did.Find("C")).Should(HavePassed())
   139  			})
   140  
   141  			It("sets up the failure node location correctly", func() {
   142  				report := reporter.Did.Find("B")
   143  				Ω(report.Failure.FailureNodeContext).Should(Equal(types.FailureNodeIsLeafNode))
   144  				Ω(report.Failure.FailureNodeType).Should(Equal(types.NodeTypeIt))
   145  				Ω(report.Failure.FailureNodeLocation).Should(Equal(report.LeafNodeLocation))
   146  			})
   147  		})
   148  
   149  		Describe("when an It panics", func() {
   150  			BeforeEach(func() {
   151  				success, _ := RunFixture("panicked it", func() {
   152  					BeforeSuite(rt.T("before-suite"))
   153  					Describe("top-level", func() {
   154  						It("A", rt.T("A", func() {
   155  							writer.Write([]byte("running A"))
   156  						}))
   157  						It("B", rt.T("B", func() {
   158  							writer.Write([]byte("running B"))
   159  							panic("boom")
   160  						}))
   161  						It("C", rt.T("C"))
   162  					})
   163  					AfterSuite(rt.T("after-suite"))
   164  				})
   165  				Ω(success).Should(BeFalse())
   166  			})
   167  
   168  			It("reports a suite failure", func() {
   169  				Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(3), NPassed(2), NFailed(1)))
   170  			})
   171  
   172  			It("runs other Its and the AfterSuite", func() {
   173  				Ω(rt).Should(HaveTracked("before-suite", "A", "B", "C", "after-suite"))
   174  			})
   175  
   176  			It("reports the It's failure", func() {
   177  				Ω(reporter.Did.Find("A")).Should(HavePassed(CapturedGinkgoWriterOutput("running A")))
   178  				Ω(reporter.Did.Find("B")).Should(HavePanicked("boom", CapturedGinkgoWriterOutput("running B")))
   179  				Ω(reporter.Did.Find("C")).Should(HavePassed())
   180  			})
   181  
   182  			It("sets up the failure node location correctly", func() {
   183  				report := reporter.Did.Find("B")
   184  				Ω(report.Failure.FailureNodeContext).Should(Equal(types.FailureNodeIsLeafNode))
   185  				Ω(report.Failure.FailureNodeType).Should(Equal(types.NodeTypeIt))
   186  				Ω(report.Failure.FailureNodeLocation).Should(Equal(report.LeafNodeLocation))
   187  			})
   188  		})
   189  
   190  		Describe("when a BeforeEach fails/panics", func() {
   191  			BeforeEach(func() {
   192  				success, _ := RunFixture("failed before each", func() {
   193  					BeforeEach(rt.T("bef-1"))
   194  					JustBeforeEach(rt.T("jus-bef-1"))
   195  					Describe("top-level", func() {
   196  						BeforeEach(rt.T("bef-2", func() {
   197  							writer.Write([]byte("bef-2 is running"))
   198  							F("fail", cl)
   199  						}))
   200  						JustBeforeEach(rt.T("jus-bef-2"))
   201  						Describe("nested", func() {
   202  							BeforeEach(rt.T("bef-3"))
   203  							JustBeforeEach(rt.T("jus-bef-3"))
   204  							It("the test", rt.T("it"))
   205  							JustAfterEach(rt.T("jus-aft-3"))
   206  							AfterEach(rt.T("aft-3"))
   207  						})
   208  						JustAfterEach(rt.T("jus-aft-2"))
   209  						AfterEach(rt.T("aft-2", func() {
   210  							writer.Write([]byte("aft-2 is running"))
   211  						}))
   212  					})
   213  					JustAfterEach(rt.T("jus-aft-1"))
   214  					AfterEach(rt.T("aft-1"))
   215  				})
   216  				Ω(success).Should(BeFalse())
   217  			})
   218  
   219  			It("reports a suite failure and a spec failure", func() {
   220  				Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(1), NPassed(0), NFailed(1)))
   221  				specReport := reporter.Did.Find("the test")
   222  				Ω(specReport).Should(HaveFailed("fail", cl), CapturedGinkgoWriterOutput("bef-2 is runningaft-2 is running"))
   223  			})
   224  
   225  			It("sets up the failure node location correctly", func() {
   226  				report := reporter.Did.Find("the test")
   227  				Ω(report.Failure.FailureNodeContext).Should(Equal(types.FailureNodeInContainer))
   228  				Ω(report.Failure.FailureNodeType).Should(Equal(types.NodeTypeBeforeEach))
   229  				Ω(report.Failure.FailureNodeContainerIndex).Should(Equal(0))
   230  			})
   231  
   232  			It("runs the JustAfterEaches and AfterEaches at the same or lesser nesting level", func() {
   233  				Ω(rt).Should(HaveTracked("bef-1", "bef-2", "jus-aft-2", "jus-aft-1", "aft-2", "aft-1"))
   234  			})
   235  		})
   236  
   237  		Describe("when a top-level BeforeEach fails/panics", func() {
   238  			BeforeEach(func() {
   239  				success, _ := RunFixture("failed before each", func() {
   240  					BeforeEach(rt.T("bef-1", func() {
   241  						F("fail", cl)
   242  					}))
   243  					It("the test", rt.T("it"))
   244  				})
   245  				Ω(success).Should(BeFalse())
   246  			})
   247  
   248  			It("sets up the failure node location correctly", func() {
   249  				report := reporter.Did.Find("the test")
   250  				Ω(report.Failure.FailureNodeContext).Should(Equal(types.FailureNodeAtTopLevel))
   251  				Ω(report.Failure.FailureNodeType).Should(Equal(types.NodeTypeBeforeEach))
   252  			})
   253  		})
   254  
   255  		Describe("when an AfterEach fails/panics", func() {
   256  			BeforeEach(func() {
   257  				success, _ := RunFixture("failed after each", func() {
   258  					BeforeEach(rt.T("bef-1"))
   259  					JustBeforeEach(rt.T("jus-bef-1"))
   260  					Describe("top-level", func() {
   261  						BeforeEach(rt.T("bef-2"))
   262  						Describe("nested", func() {
   263  							BeforeEach(rt.T("bef-3"))
   264  							It("the test", rt.T("it"))
   265  							JustAfterEach(rt.T("jus-aft-3"))
   266  							AfterEach(rt.T("aft-3", func() {
   267  								F("fail", types.NewCodeLocation(0))
   268  							}))
   269  						})
   270  						JustAfterEach(rt.T("jus-aft-2"))
   271  						AfterEach(rt.T("aft-2", func() {
   272  							writer.Write([]byte("aft-2 is running"))
   273  						}))
   274  					})
   275  					JustAfterEach(rt.T("jus-aft-1"))
   276  					AfterEach(rt.T("aft-1"))
   277  				})
   278  				Ω(success).Should(BeFalse())
   279  			})
   280  
   281  			It("reports a suite failure and a spec failure", func() {
   282  				Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(1), NPassed(0), NFailed(1)))
   283  				specReport := reporter.Did.Find("the test")
   284  				Ω(specReport).Should(HaveFailed("fail"), CapturedGinkgoWriterOutput("aft-2 is running"))
   285  			})
   286  
   287  			It("sets up the failure node location correctly", func() {
   288  				report := reporter.Did.Find("the test")
   289  				Ω(report.Failure.FailureNodeContext).Should(Equal(types.FailureNodeInContainer))
   290  				Ω(report.Failure.FailureNodeType).Should(Equal(types.NodeTypeAfterEach))
   291  				location := report.Failure.Location
   292  				location.LineNumber = location.LineNumber - 1
   293  				Ω(report.Failure.FailureNodeLocation).Should(Equal(location))
   294  				Ω(report.Failure.FailureNodeContainerIndex).Should(Equal(1))
   295  			})
   296  
   297  			It("runs the subsequent after eaches", func() {
   298  				Ω(rt).Should(HaveTracked("bef-1", "bef-2", "bef-3", "jus-bef-1", "it", "jus-aft-3", "jus-aft-2", "jus-aft-1", "aft-3", "aft-2", "aft-1"))
   299  			})
   300  		})
   301  
   302  		Describe("when multiple nodes within a given test run and fail", func() {
   303  			var clA, clB types.CodeLocation
   304  			BeforeEach(func() {
   305  				clA = types.CodeLocation{FileName: "A"}
   306  				clB = types.CodeLocation{FileName: "B"}
   307  				success, _ := RunFixture("failed after each", func() {
   308  					BeforeEach(rt.T("bef-1", func() {
   309  						writer.Write([]byte("run A"))
   310  						F("fail-A", clA)
   311  					}))
   312  					It("the test", rt.T("it"))
   313  					AfterEach(rt.T("aft-1", func() {
   314  						writer.Write([]byte("run B"))
   315  						F("fail-B", clB)
   316  					}))
   317  				})
   318  				Ω(success).Should(BeFalse())
   319  			})
   320  
   321  			It("reports a suite failure and a spec failure and only tracks the first failure", func() {
   322  				Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(1), NPassed(0), NFailed(1)))
   323  				specReport := reporter.Did.Find("the test")
   324  				Ω(specReport).Should(HaveFailed("fail-A", clA), CapturedGinkgoWriterOutput("run Arun B"))
   325  				Ω(specReport.Failure.FailureNodeType).Should(Equal(types.NodeTypeBeforeEach))
   326  				Ω(rt).Should(HaveTracked("bef-1", "aft-1"))
   327  			})
   328  		})
   329  	})
   330  
   331  	Describe("when there are multiple tests that fail", func() {
   332  		BeforeEach(func() {
   333  			success, _ := RunFixture("failed after each", func() {
   334  				It("A", func() { F() })
   335  				It("B", func() { F() })
   336  				It("C", func() {})
   337  				It("D", func() { F() })
   338  				It("E", func() {})
   339  				It("F", func() { panic("boom") })
   340  			})
   341  			Ω(success).Should(BeFalse())
   342  		})
   343  
   344  		It("reports the correct number of failures", func() {
   345  			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(6), NPassed(2), NFailed(4)))
   346  			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("C", "E"))
   347  			Ω(reporter.Did.WithState(types.SpecStateFailed).Names()).Should(ConsistOf("A", "B", "D"))
   348  			Ω(reporter.Did.WithState(types.SpecStatePanicked).Names()).Should(ConsistOf("F"))
   349  		})
   350  	})
   351  })