Are your test helpers correct -- be confident with faket.
Test helper functions taking a testing.TB
are
helpful for encapsulating common validation patterns
and DRY'ing up tests (Don't Repeat Yourself).
These helpers are not immune from bugs and as they grow in complexity and become widely used. Verifying their behaviour becomes important-- your tests' correctness depends upon them!
How do you test scenarios where the helper is expected to fail?
Using a real testing.T
would fail the test,
when you want the opposite:
the test should fail if the helper passes unexpectedly
but pass if the helper fails with the expected failures!
faket lets you validate correctness of your helpers
using a fake testing.TB
, which provides insight
into the full behaviour of the test function:
- Are all the expected errors reported?
- Did the helper
Fatal
the test? - Is the output formatted correctly with all the relevant information?
Let's test a helper used to verify a string contains expected substrings in order:
func StrContainsInOrder(t testing.TB, got string, contains ...string)
With faket, both successful and failure scenarios can be tested:
func TestStrContainsInOrder(t *testing.T) {
t.Run("correct order", func(t *testing.T) {
faket.RunTest(func(t testing.TB) {
StrContainsInOrder(t, "test helper function", "test", "helper")
}).MustPass(t)
})
t.Run("incorrect order", func(t *testing.T) {
faket.RunTest(func(t testing.TB) {
StrContainsInOrder(t, "test helper function", "helper", "test")
}).MustFail(t, `failed to find "test" in remaining string " function"`)
})
}
- Fully-featured implementation of
testing.TB
includingSkip
,Fatal
, andCleanup
. - Thoroughly tested against the real
testing.TB
to ensure correct behaviour. - Panic handling to allow validation of expected panics.
go get -u github.com/prashantv/faket
faket is only supported and tested against the 2 most recent minor versions of Go.
This library is ready for use in tests, but it's not API stable.