How to test Go function containing log.Fatal()

This is similar to “How to test os.Exit() scenarios in Go”: you need to implement your own logger, which by default redirect to log.xxx(), but gives you the opportunity, when testing, to replace a function like log.Fatalf() with your own (which does not call os.Exit(1))

I did the same for testing os.Exit() calls in exit/exit.go:

exiter = New(func(int) {})
exiter.Exit(3)
So(exiter.Status(), ShouldEqual, 3)

(here, my “exit” function is an empty one which does nothing)

Leave a Comment