Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Extra ¶
func Extra(pointersAndInjectors ...interface{}) nject.Provider
Extra is a means to obtain more than one of something needed for a test.
The extra bits may be need to be created in the middle of a pre-made injection sequence. The easiest way to handle that is to use nject.Provide() to name the injectors in the injection chain. Then you can use nject.InsertAfterNamed() to wrap the Extra() to "move" the effective location of the Extra.
Alternatively, you can avoid the pre-made injection sequences so that you explicitly add Extra in the middle.
The arguments to Extra can be two different kinds of things. First put pointers to variables that you want extra of. Then put any additional injectors that might be needed to create those things. You can nest calls to Extra inside call to Extra if you want to share some comment components.
func MustParallel ¶ added in v0.8.0
func MustParallel(t T)
MustParallel calls .Parallel() on the underlying T if it supports .Parallel. If not, it fails the test. If the input T is a ReWrapper then it will be unwrapped to find a T that supports Parallel.
func Parallel ¶ added in v0.8.0
func Parallel(t T)
Parallel calls .Parallel() on the underlying T if it supports .Parallel. If not, it logs a warning and continues without Parallel. If the input T is a ReWrapper then it will be unwrapped to find a T that supports Parallel.
func Run ¶ added in v0.8.0
Run is a helper that runs a subtest and automatically handles ReWrap logic. This should be used instead of calling t.Run directly when using logger wrappers like ReplaceLogger, BufferedLogger, or ExtraDetailLogger that support the ReWrapper interface.
Key benefits: - Works with both *testing.T and *testing.B (they have different Run signatures) - Automatically rewraps logger wrappers for subtests via ReWrap interface - Can be used with any T, whether wrapped or not
Example:
logger := ntest.BufferedLogger(t) ntest.Run(logger, "subtest", func(subT ntest.T) { // subT is automatically a properly wrapped BufferedLogger subT.Log("This will be buffered correctly") })
func RunMatrix ¶
RunMatrix uses t.Run() separate execution for each sub-test before any chains are evaluated. This forces the chains to share nothing between them. RunMatrix does not provide any default injectors other than a *testing.T that comes from a named provider (named "testing.T")
A matrix is a specific type: map[string]nject.Provider. Add those to the chain to trigger matrix testing.
Matrix values must be direct arguments to RunMatrix -- they will not be extracted from nject.Sequences. RunMatrix will fail if there is no matrix provided.
The provided T must support Run()
func RunParallelMatrix ¶
RunParallelMatrix uses t.Run() to fork into multiple threads of execution for each sub-test before any chains are evaluated. This forces the chains to share nothing between them. RunParallelMatrix does not provide any default injectors other than a *testing.T that comes from a named provider (named "testing.T"). That injector is only present if the RunT argument was actually a *testing.T
A matrix is a specific type: map[string]nject.Provider. Add those to the chain to trigger matrix testing.
t.Parallel() is used for each t.Run()
A warning about t.Parallel(): inner tests wait until outer tests finish. See https://go.dev/play/p/ZDaw054HeIN
Matrix values must be direct arguments to RunMatrix -- they will not be extracted from nject.Sequences. RunParallelMatrix will fail if there is no matrix provided.
The provided T must support Run()
Types ¶
type Cancel ¶ added in v0.4.0
type Cancel func()
Cancel is the injected type for the function type that will cancel a Context that has been augmented with AutoCancel.
type ReWrapper ¶ added in v0.7.0
type ReWrapper interface { T // ReWrap must return a T that is wrapped (with the current class) compared to it's input // This is re-applying the wrapping to get back to the type of the ReWrapper. // ReWrap only needs to care about it's own immediate wrapping. It does not need to // check if it's underlying type implements ReWrapper. ReWrap(T) T // Unwrap must return a T that is unwrapped compared to the ReWrapper. // This is providing access to the inner-T Unwrap() T }
ReWrapper allows types that wrap T to recreate themselves from fresh T This, combined with Run() and Parallel(), allows proper subtest handling in tests that wrap T.
type T ¶
type T interface { Cleanup(func()) Setenv(key, value string) Error(args ...interface{}) Errorf(format string, args ...interface{}) Fail() FailNow() Failed() bool Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) Helper() Log(args ...interface{}) Logf(format string, args ...interface{}) Name() string Skip(args ...interface{}) Skipf(format string, args ...interface{}) Skipped() bool }
T is subset of what *testing.T provides.
It is missing:
.Run - not present in ginkgo.GinkgoT() .Parallel - not present in *testing.B
func BufferedLogger ¶ added in v0.6.0
BufferedLogger creates a logger wrapper that buffers all log output and only outputs it during test cleanup if the test failed. Each log entry includes the filename and line number where the log was called. The purpose of this is for situations where go tests are defaulting to -v but output should be suppressed anyway.
If the environment variable NTEST_BUFFERING is set to "false", buffering will be turned off and the original T will be returned directly.
One advantage of using BufferedLogger over using "go test" (without -v) is that you can see the skipped tests with BufferedLogger whereas non-v go test hides the skips.
func ExtraDetailLogger ¶ added in v0.3.0
ExtraDetailLogger creates a logger wrapper that adds both a prefix and a timestamp to each line that is logged. A space after the prefix is also added.
func ReplaceLogger ¶ added in v0.3.0
ReplaceLogger creates a wrapped T that overrides the logging function. For accurate line number reporting in log output, call t.Helper() at the beginning of your logger function to mark it as a helper function.
Example:
logger := ntest.ReplaceLogger(t, func(s string) { t.Helper() // Mark this function as a helper for accurate line numbers t.Log("PREFIX: " + s) })