• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D24-Mar-2020118

LICENSEH A D24-Mar-20201.1 KiB

README.mdH A D24-Mar-20201.9 KiB

go.modH A D24-Mar-202058

testing.goH A D24-Mar-20202.2 KiB

testing_test.goH A D24-Mar-2020245

README.md

1# go-testing-interface
2
3go-testing-interface is a Go library that exports an interface that
4`*testing.T` implements as well as a runtime version you can use in its
5place.
6
7The purpose of this library is so that you can export test helpers as a
8public API without depending on the "testing" package, since you can't
9create a `*testing.T` struct manually. This lets you, for example, use the
10public testing APIs to generate mock data at runtime, rather than just at
11test time.
12
13## Usage & Example
14
15For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/go-testing-interface).
16
17Given a test helper written using `go-testing-interface` like this:
18
19    import "github.com/mitchellh/go-testing-interface"
20
21    func TestHelper(t testing.T) {
22        t.Fatal("I failed")
23    }
24
25You can call the test helper in a real test easily:
26
27    import "testing"
28
29    func TestThing(t *testing.T) {
30        TestHelper(t)
31    }
32
33You can also call the test helper at runtime if needed:
34
35    import "github.com/mitchellh/go-testing-interface"
36
37    func main() {
38        TestHelper(&testing.RuntimeT{})
39    }
40
41## Versioning
42
43The tagged version matches the version of Go that the interface is
44compatible with. For example, the version "1.14.0" is for Go 1.14 and
45introduced the `Cleanup` function. The patch version (the ".0" in the
46prior example) is used to fix any bugs found in this library and has no
47correlation to the supported Go version.
48
49## Why?!
50
51**Why would I call a test helper that takes a *testing.T at runtime?**
52
53You probably shouldn't. The only use case I've seen (and I've had) for this
54is to implement a "dev mode" for a service where the test helpers are used
55to populate mock data, create a mock DB, perhaps run service dependencies
56in-memory, etc.
57
58Outside of a "dev mode", I've never seen a use case for this and I think
59there shouldn't be one since the point of the `testing.T` interface is that
60you can fail immediately.
61