Documentation ¶
Overview ¶
Package marker provides a slog.Handler and an associated API for implementing explicit code coverage marks for *linking* source code and tests together.
In production code, you use your logger as normal, and can use it to say "this should be covered by a test". In test code, you can then assert that a _specific_ test covers a specific log line.
The purpose of this is to help with test maintenance over time in larger projects. Large projects often have a lot of tests. Finding the tests for a specific piece of code, and vice versa, can be a challenge. This package provides a simple solution to that problem by leveraging your existing logger, and simply enabling the use of `grep` to search for a corresponding test. For example, if you see `logger.Debug("request sent, waiting on response")` in the code, you can grep for that log message and immediately find the test that goes with that code path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler returns a slog.Handler implementation to help trace tests to source code. In a test environment, reported by testing.Testing, the slog.Handler returned records that a log message was hit.
In a test, Check is used to say that the code under test should log a specific message. It returns a Mark where Mark.ExpectHit is expected to be called after the code under test is ran.
In non-tests(i.e. normal code operation), this recording of Mark's is a no-op.
Types ¶
type Mark ¶
type Mark struct {
// contains filtered or unexported fields
}
func Check ¶
Check stores the given mark name in global state to be subsequently asserted it was hit with Mark.ExpectHit.
Check will panic if not used in a testing environment, as reported by testing.Testing.
func (Mark) ExpectHit ¶
ExpectHit returns an error if the stored name on Mark was not hit. ExpectHit requires Check to have been called first with the mark name that you expect to have been logged in the function under test.
ExpectHit will panic if not used in a testing environment, as reported by testing.Testing.