It blocks in a for loop

Skip to first unread message

Jun 5, 2015, 10:15:41 AM6/5/15
Can Ginkgo It blocks be put inside a for loop? I have a table of test cases (a slice of structs) which I'd like to iterate through:

var _ = Describe("...", func() {

var testcases = []struct{
{"start", "stop"},
    {"stop", "start"},

for i := range testcases {
:= testcases[i]

It("...", func() {
// Run test case

This compiles and runs but it runs just the last test case multiple times. Is this expected behaviour?

I can put the for loop inside the It block and it works as expected but then I can't use BeforeEach (since there's only one It block) and it can be difficult to work out which test case is failing.

Onsi Fakhouri

Jun 5, 2015, 11:04:51 AM6/5/15
Yep this should totally work. Have done it on numerous occasions where table driven tests made sense.  This is equivalent to unrolling the for loop and manually typing in the N Its that the for loop would generate.

Are you sure you're referencing 'tc' in your It closure and not 'i'?  Because it's a closure you'll get the last value of 'i'

If you point me at some code I might be able to help some more.

You received this message because you are subscribed to the Google Groups "Ginkgo And Gomega" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

Thorsten von Eicken

Aug 20, 2015, 2:27:56 PM8/20/15
to Ginkgo And Gomega
I had a reference to 'i' that I overlooked. Grrr.

Friedrich Große

Oct 24, 2015, 4:57:06 AM10/24/15
to Ginkgo And Gomega
Maybe a little late but for future generations of gophers I would like to add the link to Table Driven Tests here :)
Reply all
Reply to author
0 new messages