Go advanced testing tips & tricks
Povilas Versockas
5324

It’s very bad practices

Trick 3:

Never usedefer in tests in that way:

func TestDeferFatal(t *testing.T) {
defer helperFatal(t)
t.Fatal("Test fatal")
}

func helperFatal(t *testing.T) {
log.Fatal("Helper fatal")
}

Output:

$ go test ./test --test.run=TestDeferFatal
2017/07/05 14:03:54 FATAL: Helper fatal

Trick 4:

If your test needs git, you can’t skip these test. If not, you can’t skip these test, because you can’t skip test by reason of test environment which not refers to the test.

Trick 5:

Notice, which package is tested by that way. Surprise, it’s os/exec! And it’s the only reason, why the trick can exist.

Like what you read? Give Alexey Makhov a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.