Upon submitting unit tests (or any code) to GitHub, GitHub Actions will trigger a workflow script. During the execution, it's checking if all unit tests pass. basically it's executing go test ./...
command and runs all tests there are.
If any of the tests fail I am not able to merge changes. Which makes sense. But should I write all tests in a way they will pass those checks but at the same time return an error?
pseudocode
testFuncForError()
got := actualError
want := wantedError
if got == want -> pass // we got the error
this test will pass even though we got the error, and the checks will pass.
but
if got != want
fmt.Printf("got %v, expected %v", got, want) // this will fail
Or for example,
package math
import "testing"
type addTest struct {
arg1, arg2, expected int
}
var addTests = []addTest{
addTest{2, 3, 5},
addTest{4, 8, 12},
addTest{6, 9, 15},
addTest{3, 10, 20},
}
func TestAdd(t *testing.T){
for _, test := range addTests{
if output := Add(test.arg1, test.arg2); output != test.expected {
t.Errorf("Output %q not equal to expected %q", output, test.expected)
}
}
}
In this case, the last Case addTest{3, 10, 20},
will return an error and the test will fail, which would mean that the checks will fail as well. How to test for error but make the test pass?