Jakub Jarosz

Security, Systems, Network & Electrical Automation

50 Go Testing Mistakes

2025-09-16 Go Books Jakub Jarosz

Hiding surprises in test helpers

Comparing error strings is a bad idea. However, hiding the comparison deep within the comparer function is even worse.

It hides deep the logic responsible for validating functions in tests and can give a false feeling of security.

Let’s find out what’s wrong with the function below and find out how we can improve it.

func errorComparer(e1, e2 error) bool {
	if e1 == nil || e2 == nil {
		return errors.Is(e1, e2)
	}
	return e1.Error() == e2.Error()
}