4.0 KiB
Writing a test is just like writing a function, with a few rules
-
It needs to be in a file with a name like
xxx_test.go -
The test function must start with the word
Test -
The test function takes one argument only
t *testing.T -
In order to use the
*testing.Ttype, you need toimport "testing", like we did withfmtin the other file
func TestHello(t *testing.T) {
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
})
t.Run("empty string defaults to 'world'", func(t *testing.T) {
got := Hello("")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
}
func assertCorrectMessage(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
In your Go code, t *testing.T is used within your test function TestHello and the helper function assertCorrectMessage for several reasons related to writing tests in Go:
-
Access to Testing Methods:
t *testing.Tis a pointer to Go's testing framework'sTtype, which provides methods for controlling test execution and logging. By passingtinto your test functions, you gain access to methods such ast.Errorf,t.Fatalf,t.Run, andt.Helper. -
Running Subtests: The
t.Runmethod allows you to define subtests or table-driven tests within your test function. This approach makes your tests more organized and can be useful for testing different scenarios or inputs for the same function. Each call tot.Runcan be considered as a separate test case within the broader test function. -
Error Reporting and Logging: The methods on
*testing.Tliket.Errorfare used for reporting errors. When you callt.Errorf, it logs the error message and marks the test as failed but continues execution. This is useful for running multiple checks within a single test and getting a report on all failures. -
Helper Function: The
t.Helpermethod in yourassertCorrectMessagefunction is used to mark that function as a test helper. When this method is called, the function is skipped when reporting where an error occurred, making the error output more readable. It tells the test framework that the actual place of interest is where the helper function was called, not inside the helper function itself. -
Interface
testing.TB: In yourassertCorrectMessagefunction, you usetesting.TBinstead of*testing.T. Thetesting.TBis an interface that is implemented by both*testing.Tand*testing.B(whereBis for benchmarks). Usingtesting.TBmakes your helper function more flexible because it can be used with both testing and benchmarking.
In summary, t *testing.T and testing.TB are essential for structuring your tests, running subtests, reporting errors, and making your test code more maintainable and flexible in Go.
For helper functions, it's a good idea to accept a testing.TB which is an interface that *testing.T and *testing.B both satisfy, so you can call helper functions from a test, or a benchmark (don't worry if words like "interface" mean nothing to you right now, it will be covered later).
t.Helper() is needed to tell the test suite that this method is a helper. By doing this when it fails the line number reported will be in our function call rather than inside our test helper. This will help other developers track down problems easier. If you still don't understand, comment it out, make a test fail and observe the test output. Comments in Go are a great way to add additional information to your code, or in this case, a quick way to tell the compiler to ignore a line. You can comment out the t.Helper() code by adding two forward slashes // at the beginning of the line. You should see that line turn grey or change to another color than the rest of your code to indicate it's now commented out.