Home > Mobile >  How to run all test cases even if one test case fails
How to run all test cases even if one test case fails

Time:07-20

func Test_something(t *testing.T) {
    // TEST CASE1: pass an array 
      // some logic here

    // TEST CASE2: pass an EMPTY array --> this will cause test to fail
      // some logic here

    // TEST CASE3: pass something else 
      // some logic here

I am writing some unit tests but I am not sure if it's possible to run a test Test_something that has several test cases without stopping the execution of other test cases if one fails. Or does it even make sense?

In console I would like to see something like this.

TESTCASE1: SUCCESS <message>
TESTCASE2: FAIL <message>
TESTCASE3: SUCCESS <message>

At the moment I get something like this:

TESTCASE1: SUCCESS <message>
TESTCASE2: FAIL <message>

It will naturally stop executing after TESTCASE2 fails.

CodePudding user response:

with t *testing.T you may call:

  • t.Errorf(...): it will not stop next tests.
  • t.Fatalf(...): it will stop next tests.

See official doc.

CodePudding user response:

You can use subtest with the help of the testing.T.Run function. It allows to gather several test cases together and have separate status for each of them.

func TestSomething(t *testing.T) {
    t.Run("first test case", func(t *testing.T) {
        // implement your first test case here
    })
    t.Run("second test case", func(t *testing.T) {
        // implement your second test case here
    }
}
  • Related