Behavior-stubbing hooks that are compiled out of production builds with -tags notest.
Pre-1.0, maintained on a best-effort basis. See Project status.
In test cases, you sometimes need to exercise error conditions returned by your dependencies that are hard to reproduce in unit tests. The solution to reproduce those scenarios typically involves one of the following options:
- Doing interaction tests with mocks
- Creating a fake version of the dependency
Both of these can however be hard when the interaction with the dependency is complicated (e.g. testing database/sql query interaction which involves many steps). This package implements an alternative where instead of replacing your dependencies you use them as is in tests and just add hooks to inject errors / failures.
For example, if you are testing:
func MyFunc() (string, error) {
// ... other code ...
result, err := somedependency.DoSomething()
if err != nil {
// error handling like:
return "", err
}
// ... other code ...
}Using this library you can do:
var DoSomethingErrHook = testhooks.NewErrHook()
func MyFunc() (string, error) {
// ... other code ...
result, err := somedependency.DoSomething()
err = DoSomethingErrHook.Apply(err) // <------ added line
if err != nil {
// error handling like:
return "", err
}
// ... other code ...
}Then in tests you can simulate the error condition with:
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMyFunc(t *testing.T) {
fakeErr := errors.New("fake error")
// Will make DoSomethingErrHook.Apply return fakeErr.
// This change will be reverted at the end of the test.
DoSomethingErrHook.WithError(fakeErr).SetupInTest(t)
_, err := MyFunc()
assert.Equal(t, fakeErr, err)
}Similar to the case for errors, there are times we want to be able to inject values in a test. One example is if we have something that is supposed to be a constant value that we want to change just during tests. The solution usually involves making the value not a constant and adding get/set methods to change it during tests.
To solve for that, we can generalize the errhook concept mentioned in the previous section to arbitrary values.
For example, if you are testing:
const maxDBResults = 1000
func MyFunc() {
results := FetchFromDb()
if len(results) >= maxDBResults {
// do something
} else {
// do something else
}
}And in tests you would like to change maxDBResults to exercise the two branches, using this library you can do:
var maxDBResultsValueHook = testhooks.NewValueHook[int]()
func maxDBResults() int {
return maxDBResultsValueHook.Apply(1000)
}
func MyFunc() {
results := FetchFromDb()
if (len(results) >= maxDBResults()) {
// do something
} else {
// do something else
}
}Then in tests you can change the value with:
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMyFunc(t *testing.T) {
// Change maxDBResults to 3.
// This change will be reverted at the end of the test.
maxDBResultsValueHook.WithValue(3).SetupInTest(t)
MyFunc()
// do assertions ...
}The alternative if you didn't use this library would be to make maxDBResults a var and change it in tests but making sure you revert it back to the original value.
By default the hooks return a WithSetup object that you can call .SetupInTest(t) that will run the setup immediately but also schedule to clean up any changes that were made at the end of the test. There is also an alternative .RunFunc(func() {}) in case you want the change to the hook to apply just within a small block.
Both modes remove exactly the activation they created: RunFunc when the block returns, and SetupInTest when the given test's cleanup runs. They never remove other activations on the same hook. The latest active activation wins, so overrides can be nested; a later activation does not get torn down by an earlier one.
Calling RunFunc or SetupInTest concurrently on the same hook from multiple goroutines is not supported; behavior is undefined.
So in the example from "Injecting values in tests" section above, you could write:
func TestMyFunc(t *testing.T) {
// Change maxDBResults to 3 within the func() {} block
// This change will be reverted once RunFunc returns.
maxDBResultsValueHook.WithValue(3).RunFunc(func() {
MyFunc()
// do assertions ...
})
}Advantages:
- For ErrHooks, avoids the need to create fakes/mocks for dependencies just to test error cases. This can be particularly useful when the dependencies are hard to fake/replicate.
- Avoids the need for you to clean up yourself any changes to values in tests.
- Related to the above, allows you to achieve higher levels of code coverage with less effort.
- When built with the
notestbuild tag, there should be no overhead to using the hooks (this is discussed in the Mitigating runtime cost of the hooks section).
Disadvantages:
- Extra lines in your code for calling the hooks.
- Need to set up your production build with the
notesttag to reduce runtime costs of testhooks.
To mitigate runtime costs introduced by the hooks, this library leverages Go build tags and the inline behavior of the Go compiler to strip away instructions related to the test hooks themselves.
When the code is built with the notest build tag, the Apply functions of the hooks are replaced with basic implementations that just return back the value they were given in. This allows the Go compiler to inline the method and basically eliminate any runtime cost of using the hooks. The code generated assembly should be roughly equivalent to that generated by the same code without the call to the Apply method of the hooks.
By default (when the notest build tag is not used), it assumes the code being built may be used in tests, so it includes implementations of the Apply methods with the branches necessary for injecting errors/values.
The default is "on" so go test works without extra flags. If the tag is omitted from a production build, the hooks remain inert unless activated, but their Apply branches can add a small runtime cost.
However production binaries should be built using the notest tag like the following:
go build -tags notest ./...To verify that this mitigation works, we run some checks on the disassembled code. More details can be found in disassemblychecks.
Requires Go 1.25 or later.
Tests also require Python 3 for the disassembly check; the library builds without it.
make testA variant of this library is used at Instacart. We're publishing it so others can use, adapt, and build on the ideas.
This is not an officially supported Instacart product and comes with no commitment to ongoing maintenance. Fixes and improvements are made on a best-effort basis, and the repository may be archived in the future.
The API is pre-1.0 and may change between minor versions; breaking changes will be noted in the release notes.
Licensed under the Apache License 2.0. See LICENSE.