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

     1  package internal_integration_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Shuffling Tests", func() {
    11  	var fixture = func() {
    12  		Describe("container-a", func() {
    13  			It("a.1", rt.T("a.1"))
    14  			It("a.2", rt.T("a.2"))
    15  			It("a.3", rt.T("a.3"))
    16  			It("a.4", rt.T("a.4"))
    17  		})
    18  
    19  		Describe("container-b", func() {
    20  			It("b.1", rt.T("b.1"))
    21  			It("b.2", rt.T("b.2"))
    22  			It("b.3", rt.T("b.3"))
    23  			It("b.4", rt.T("b.4"))
    24  		})
    25  
    26  		Describe("ordered-container", Ordered, func() {
    27  			It("o.1", rt.T("o.1"))
    28  			It("o.2", rt.T("o.2"))
    29  			It("o.3", rt.T("o.3"))
    30  			It("o.4", rt.T("o.4"))
    31  		})
    32  
    33  		It("top.1", rt.T("top.1"))
    34  		It("top.2", rt.T("top.2"))
    35  		It("top.3", rt.T("top.3"))
    36  		It("top.4", rt.T("top.4"))
    37  	}
    38  
    39  	Describe("the default behavior", func() {
    40  		It("shuffles top-level containers and its only, preserving the order of tests within containers", func() {
    41  			orderings := []string{}
    42  			uniqueOrderings := map[string]bool{}
    43  			for i := 0; i < 10; i += 1 {
    44  				conf.RandomSeed = int64(i)
    45  				RunFixture("run", fixture)
    46  				order := strings.Join(rt.TrackedRuns(), "")
    47  				rt.Reset()
    48  
    49  				Ω(order).Should(ContainSubstring("a.1a.2a.3a.4"), "order in containers should be preserved")
    50  				Ω(order).Should(ContainSubstring("b.1b.2b.3b.4"), "order in containers should be preserved")
    51  				Ω(order).Should(ContainSubstring("o.1o.2o.3o.4"), "order in containers should be preserved")
    52  				orderings = append(orderings, order)
    53  				uniqueOrderings[order] = true
    54  			}
    55  			Ω(orderings).Should(ContainElement(Not(ContainSubstring("top.1top.2top.3top.4"))), "top-level its should be randomized")
    56  			Ω(uniqueOrderings).ShouldNot(HaveLen(1), "after 10 runs at least a few should be different!")
    57  		})
    58  	})
    59  
    60  	Describe("when told to randomize all specs", func() {
    61  		It("shuffles all its, but preserves ordered containers", func() {
    62  			conf.RandomizeAllSpecs = true
    63  			orderings := []string{}
    64  			uniqueOrderings := map[string]bool{}
    65  			for i := 0; i < 10; i += 1 {
    66  				conf.RandomSeed = int64(i)
    67  				RunFixture("run", fixture)
    68  				order := strings.Join(rt.TrackedRuns(), "")
    69  				rt.Reset()
    70  
    71  				Ω(order).Should(ContainSubstring("o.1o.2o.3o.4"), "order in containers should be preserved")
    72  				orderings = append(orderings, order)
    73  				uniqueOrderings[order] = true
    74  			}
    75  			Ω(orderings).Should(ContainElement(Not(ContainSubstring("top.1top.2top.3top.4"))), "top-level its should be randomized")
    76  			Ω(orderings).Should(ContainElement(Not(ContainSubstring("a.1a.2a.3a.4"))), "its in containers should be randomized")
    77  			Ω(orderings).Should(ContainElement(Not(ContainSubstring("b.1b.2b.3b.4"))), "its in containers should be randomized")
    78  			Ω(uniqueOrderings).ShouldNot(HaveLen(1), "after 10 runs at least a few should be different!")
    79  		})
    80  	})
    81  
    82  	Describe("when given the same seed", func() {
    83  		It("yields the same order", func() {
    84  			for _, conf.RandomizeAllSpecs = range []bool{true, false} {
    85  				uniqueOrderings := map[string]bool{}
    86  				for i := 0; i < 10; i += 1 {
    87  					conf.RandomSeed = 1138
    88  					RunFixture("run", fixture)
    89  					order := strings.Join(rt.TrackedRuns(), "")
    90  					rt.Reset()
    91  					uniqueOrderings[order] = true
    92  				}
    93  
    94  				Ω(uniqueOrderings).Should(HaveLen(1), "all orders are the same")
    95  			}
    96  		})
    97  	})
    98  })