The go test tool is like the compiler. To the compiler something either compiles or doesn't, there are no warnings. I think the closest thing you're going to get is to use t.Skip. It will stop execution of the current test but does not mark it as failed. You will not see anything in the output of go test
however so you have to use go test -v
.
Here's an example package that uses t.Skipf
if the addExternal
function fails.
package app
import "testing"
func add(a, b int) int {
return a + b
}
func addExternal(a, b int) int {
return 4
}
func divide(a, b int) int {
return a / b
}
func TestThing(t *testing.T) {
got := add(1, 2)
want := 3
if got != want {
t.Errorf("add(1, 2) = %d, want %d", got, want)
}
}
func TestExternalThing(t *testing.T) {
got := addExternal(3, 4)
want := 7
if got != want {
t.Skipf("addExternal(3, 4) = %d, want %d", got, want)
}
}
func TestAnotherThing(t *testing.T) {
got := divide(6, 3)
want := 2
if got != want {
t.Errorf("divide(6, 3) = %d, want %d", got, want)
}
}
And here's the output from running that. Note the return status is 0 and the package is considered to have passed
$ go test -v
=== RUN TestThing
--- PASS: TestThing (0.00s)
=== RUN TestExternalThing
--- SKIP: TestExternalThing (0.00s)
app_test.go:29: addExternal(3, 4) = 4, want 7
=== RUN TestAnotherThing
--- PASS: TestAnotherThing (0.00s)
PASS
ok github.com/jcbwlkr/app 0.006s
$ echo $?
0
Note though that if I change the t.Skipf
to t.Errorf
or t.Fatalf
I get this output
$ go test -v
=== RUN TestThing
--- PASS: TestThing (0.00s)
=== RUN TestExternalThing
--- FAIL: TestExternalThing (0.00s)
app_test.go:29: addExternal(3, 4) = 4, want 7
=== RUN TestAnotherThing
--- PASS: TestAnotherThing (0.00s)
FAIL
exit status 1
FAIL github.com/jcbwlkr/app 0.005s
$ echo $?
1
The other tests in the package are still ran. If I was testing multiple packages such as with go test -v ./...
I believe they would also still be ran.