Golang std testing: Difference between revisions
From wikinotes
No edit summary |
|||
Line 75: | Line 75: | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
func TestHello(t *testing.T) { | func TestHello(t *testing.T) { | ||
tcases := []struct { // <-- slice of structs containing testdata | |||
test string | test string | ||
name string | name string | ||
Line 82: | Line 82: | ||
{ test: "NilName", name: nil, } } | { test: "NilName", name: nil, } } | ||
for _, | for _, tcase := range tcases { | ||
t.Run( | t.Run(tcase.test, func(t, *testing.T) { // <-- t.Run() evaluates each case | ||
res := Hello( | res := Hello(tcase.name) | ||
if res != | if res != tcase.expects { | ||
t.Errorf("Failed because...") | t.Errorf("Failed because...") | ||
} | } |
Revision as of 19:37, 10 July 2022
Go ships with a minimalist test suite.
Documentation
testing
https://pkg.go.dev/testing@go1.18.3
Usage
go test -run # run all tests go test -run Foo # run top-level tests containing 'Foo'
Example
The builtin go test framework is fairly minimalist.
Tests are just functions, you can loop them if useful.
Tests are typically kept alongside code.
// myproject/mypackage/mylib.go package mypackage func Hello(name string) string { return "Hello, " + name }// myproject/mypackage/mylib_test.go package mypackage import "testing" func TestHello(t *testing.T) { res := Hello("Adam") if res != "Hello, Adam" { t.Errorf("Hello() result did not match") } }
Assertions
There are no assertions, you are responsible for tests and messages.
func TestHello(t *testing.T) { // log message and fail (but continue executing) t.Errorf("An expectation was not satisfied") t.Fail() // mark test as failed, but continue t.FailNow() // mark test as failed and stop executing t.Skip("Reason") // log, and stop executing t.TempDir() // provides a tempdir that is deleted once test finishes running }
Subtests
Evaluate subtests under one function using
t.Run("TESTNAME", ...)
.func TestHello(t *testing.T) { tcases := []struct { // <-- slice of structs containing testdata test string name string expects string }{ { test: "ValidName", name: "Adam", }, { test: "NilName", name: nil, } } for _, tcase := range tcases { t.Run(tcase.test, func(t, *testing.T) { // <-- t.Run() evaluates each case res := Hello(tcase.name) if res != tcase.expects { t.Errorf("Failed because...") } } } }You can also implement setup/teardown this way using
defer
functions.
Benchmarking
There are tools for benchmarking. See docs
Fuzzing
There are tools for fuzzing tests. See docs