-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
53 lines (45 loc) · 1.57 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package zaptest_test
import (
"testing"
"github.com/fgrosse/zaptest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/zap"
)
// TestLogger shows how to use a zaptest with normal unit tests.
// Run `go test -v`to see the output of this test.
func TestLogger(t *testing.T) {
l := zaptest.Logger(t)
l.Debug("This is a debug message, debug messages will be logged as well")
l.Info("Logs will not be shown during normal test execution")
l.Warn("You can see all log messages of a successful run by passing the -v flag")
l.Error("Additionally the entire log output for a specific unit test will be visible when a test fails")
}
// BenchmarkLogger demonstrates that logging in standard benchmarks works just
// like logging in normal unit tests.
func BenchmarkLogger(b *testing.B) {
l := zaptest.Logger(b)
l.Info("Logging in benchmarks works the same way")
}
// TestLoggerWriter show how to use the LoggerWriter function for ginkgo tests.
// Run `ginkgo -v`to see the output of this test.
func TestLoggerWriter(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Ginkgo Example Suite")
}
// TestedType is an example of some type you want to test.
type TestedType struct {
log *zap.Logger
}
// DoStuff is an example function of the TestedType which uses a logger.
func (tt *TestedType) DoStuff() error {
tt.log.Debug("Doing stuff")
return nil
}
// Describe the TestedType using ginkgo/gomega.
var _ = Describe("TestedType", func() {
It("should do stuff", func() {
tt := &TestedType{log: zaptest.LoggerWriter(GinkgoWriter)}
Expect(tt.DoStuff()).To(Succeed())
})
})