Can I Iterate the tests for ginkgo

1.5k Views Asked by At

Is it possible to have a variable number of test cases. Let's say I have a BeforeSuite function which calculates the values in the array.

Then based on the length of the array, I want to run one test per array element.

var _ = Describe("Outer", func() {
    var stringArray []string

    BeforeSuite(func() {
        stringArray = []string{"a", "b", "c", "d", "e"} // can vary every time i run the suite
    })


    Describe("test 1", func() {
        for _, s := range stringArray {
            It("multiple tests", func() {
                print(s)
                Expect(s).ToNot(Equal("f"))
            })
        }
    })
})

I do understand the way Ginkgo runs. It does 2 parses. First it run all the non-spec elements and then runs the spec elements.

There was an issue on github https://github.com/onsi/ginkgo/issues/462 which suits my use case, but the OP misunderstood and it took a different turn.

1

There are 1 best solutions below

0
On

Yes, It is possible. I have tried similar before without any issue. Every time the loop actually run should be a new test case. So, give it a new name to distinguish it from other when one test actually fails.

var _ = Describe("Outer", func() {
    var stringArray []string

    BeforeSuite(func() {
        stringArray = []string{"a", "b", "c", "d", "e"} // can vary every time i run the suite
    })


    Describe("test 1", func() {
        for _, s := range stringArray {
          When("Input is "+s, func(){
             It("match the output", func() {
                print(s)
                Expect(s).ToNot(Equal("f"))
            })
          })
        }
    })
})

Did you face any issue in your current code?