I have a scenario running testcases in GO where in I know that a testfile for eg: first_test.go will pass after second or third attempt ,
assuming that it is invoking a connection to a database or calling a REST service or any other typical scenario.
Was going through the options available in the $go test
command ,but no parameters are available to many tries. Is there any way of implementing the tries for a file or calling a method from a static file with contains any method to try 3-4 times, like for this typical file scenario:
func TestTry(t *testing.T) {
//Code to connect to a database
}
One idiom is to use build flags. Create a special test file only for integration test and add
// +build integration
package mypackage
import testing
Then to run the tests for integration run :
go test -tags=integration
And then you can add logic
// +build integration
package testing
var maxAttempts = flag.Int(...)
func TestMeMaybe(t *testing.T){
for i :=0 ; i < *maxAttempts; i++ {
innerTest()
}
}
No, this would be very strange: What good is a test if it randomly succeeds sometimes?
Why don't you "try" yourself inside the test? The real test either passes or fails and you handle your knowledge about "I need to 'try' calling this external resource n times to wake it up."
That's not the way test are meant to work: a test is here to tell you if your code is working as expected, not tell if an external resource is available.
The simplest way to do it when using an external resource (a webservice or api, for example) is to mock out it's functionnalities by making fake calls that return a valid response, then run your code on that. Then you will be able to test your code.