Closed yatindrav closed 1 year ago
I believe in the above case all iterations are running the same tests.
hey @yatindrav the issue is that the i
variable is captured by the It
closure - any changes to i
are therefore seen by all closures and you end up with the final i
value... so all the iterations end up running the last test. Here's a non-Ginkgo example of this behavior:
names := []string{"Bob", "Jim", "Jane", "Alice"}
callbacks := []func(){}
for i := range names {
callbacks = append(callbacks, func() {
fmt.Println(names[i])
})
}
for _, f := range callbacks {
f()
}
will print:
Alice
Alice
Alice
Alice
The common way to fix this is:
Context("Executing Sanity tests", func() {
for i := range Tests {
i := i // <== this will create a local copy of i that will no longer change between iterations
It("Now Running test " + Tests[i].TestCmd, func() { //report shows the exact command
var command *exec.Cmd
if Tests[i].TestParam != "" { //Tests[i] will now be correct
command = exec.Command(Tests[i].TestCmd, Tests[i].TestParam)
} else {
command = exec.Command(Tests[i].TestCmd)
}
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Ω(err).NotTo(HaveOccurred())
session.Wait()
Eventually(session).WithTimeout(time.Second * 10).Should(gexec.Exit(0)) //with exit code 0
})
}
})
Thanks onsi it worked!
Closing it.
I've written the following code: type TestRun struct { TestCmd string TestParam string } Tests []TestRun
And if run this using the following command: ginkgo --json-report=report.json --output-dir=./log
It saves the output of last command for all the commands in the report. What is wrong in this code? Quick response will help.