2

In Go, is there a way to abort a suite of tests early if one of them fails?

I am using stretchr/testify suites but this just builds on the basic go testing functionality.

Some options I have considered:

  • I looked at setting testing.failFast but it is not exported.
  • os.Exit() is not recommended, because it could mess up the test output among other things.
  • stop on first failure is not sufficient as the first failure might not be in a critical test

I can add my own flag and then add to each test:

if criticalTestFailed {
    t.skipTest()
}

But this is repetitive and annoying boilerplate to add to each test. What I want is something like:

func (suite *MySuite) TestCritcalTest() {
   t := MySuite.T()
   defer func() {
      if t.Failed() {
          MySuite.SkipRemainingTests() //does not exist    
      }
   }()

   // some tests here...
}

Is there a common practice here?

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
  • 2
    Does this answer your question? [Stop on first test failure with \`go test\`](https://stackoverflow.com/questions/32046192/stop-on-first-test-failure-with-go-test) – bereal May 13 '20 at 09:42
  • Or do you want it only for specific tests? – bereal May 13 '20 at 09:43
  • That doesn't help as the first test to fail is not necessarily a critical test. – Bruce Adams May 13 '20 at 09:56
  • `panic()` may be better than `os.Exit()` as at least deferred functions are run. I would have thought testing would continue after a test panics but it does not seem to in a test I did. – Andrew W. Phillips May 13 '20 at 11:27
  • 1
    You can use [`FailNow`](https://pkg.go.dev/github.com/stretchr/testify/assert?tab=doc#FailNow) – SystemGlitch May 13 '20 at 11:49
  • I don't think that it is possible directly. On the other hand, you might consider wrapping the running of the tests in a script (shell, make, whatever). The script would run first the critical test and stop if that one fails, or continue running the other tests if the critical one passes. – marco.m May 13 '20 at 11:56
  • @SystemGlitch FailNow terminates the current test not the whole test suite. – Bruce Adams May 14 '20 at 15:31

1 Answers1

2

The new go test -failfast flag disables running additional tests after any test fails. Note that tests running in parallel with the failing test are allowed to complete. --from the golang website

go test -failfast
Vedant
  • 422
  • 1
  • 3
  • 21