Documentation ¶
Overview ¶
Package test includes small test helpers, as is appropriate for this snippets module. It is envisaged that it may well outgrow this module at which point it will move to foxygo.at/test.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Env = Environ{}
Env is a global Environ variable for simpler use. Since the OS environment is global, using a global test.Environ is rarely a problem. Use is as simple as:
func TestFoo(t *testing.T) { test.Env.Set("FOO", "BAR").Unset("BAZ") defer test.Env.Restore() ... test, test, test ... }
Functions ¶
func ReadJSON ¶ added in v0.0.37
ReadJSON decodes JSON from a file into a value. If it fails to open the given filename or decode the contents of the file, ReadJSON will call t.Fatal with the error.
The type of the first parameter as testing.TB allows it to be used from both tests and benchmarks, passing either a *testing.T or *testing.B.
cfg := Config{} test.ReadJSON(t, "testdata/config.json", &cfg)
Types ¶
type Environ ¶
Environ is a frontend to the OS environment that keeps track of the previous value of an environment variable before setting or unsetting it. Later, Restore() can be called to reset all modified variables back to their original values and their presence.
It is intended to be used in test cases where a particular starting environment is needed and to ensure that it is reset back to the starting state at the end.
The operations on Environ are not concurrency-safe as the global OS environment is also not concurrency-safe. You should not use Environ in test functions that are marked t.Parallel().
The methods on Environ return the receiver so environment setup can be chained:
e := test.Environ{}.Set("FOO", "BAR").Unset("BAZ") defer e.Restore()
func (Environ) Restore ¶
func (e Environ) Restore()
Restore restores all the saved values from Set and Unset back to the original values and presence. When Restore returns, all saved values will be forgotten. This permits the Environ to be reused. If Restore is unable to set or unset any variables back to their original state, it will panic.
func (Environ) Set ¶
Set sets key to value in the OS environment, saving the previous value and presence. It returns the receiver so calls can easily be chained. If key or value are invalid (as per os.Setenv), this method will panic.