It blocks in a for loop

373 views
Skip to first unread message

alexd...@gmail.com

unread,
Jun 5, 2015, 10:15:41 AM6/5/15
to ginkgo-a...@googlegroups.com
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{
    startState
string
    endState
string
 
}{
   
{"start", "stop"},
    {"stop", "start"},
 
}

 
for i := range testcases {
    tc
:= 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

unread,
Jun 5, 2015, 11:04:51 AM6/5/15
to ginkgo-a...@googlegroups.com
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.


Onsi
--
https://github.com/onsi/ginkgo
https://github.com/onsi/gomega
---
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 ginkgo-and-gom...@googlegroups.com.
To post to this group, send email to ginkgo-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/ginkgo-and-gomega.
To view this discussion on the web visit https://groups.google.com/d/msgid/ginkgo-and-gomega/563fa846-47d7-4008-936d-74013e995a23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thorsten von Eicken

unread,
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

unread,
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
Forward
0 new messages