rs / rest-layer

REST Layer, Go (golang) REST API framework
http://rest-layer.io
MIT License
1.26k stars 114 forks source link

Set up a test-context for unit-test logging (internal) #239

Closed smyrman closed 5 years ago

smyrman commented 5 years ago

In our own project, we have set up an util functoion TestContext(tb testing.TB) context.Context, that is used to retrieve the initial (backgrond) context used in tests. We let this context contain a logger that writes formatted output to the test instance (via t.Log(...)). A context is fetched at the start of each sub-test that calls code with takes a context parameter. We usezerolog` with a console logger for this.

I can probably replicate/copy this code to internal/testutil and replace context.Background() and context.TODO() in relevant tests.

The benifit of this, is that failing tests that runs code that produce log entries, get's easier to debug.

smyrman commented 5 years ago

We have experienced issues with this approach (with the race detector), and I have decided to abandon the idea.