Go Testing: Difference between revisions
Line 15: | Line 15: | ||
=Test Types in Go= | =Test Types in Go= | ||
==Unit Tests== | ==Unit Tests== | ||
<code>somepkg_test.go</code> files should contain exclusively unit tests, not integration or system tests. Integration tests should be stored in <code>*_integration_test.go</code> files and system tests in <code>*_system_test.go</code> files. <span id='unit_test_Build_Tag'></span>Optionally, unit test files may include the <code>unit_test</code> [[Go_Build_Tags# | <code>somepkg_test.go</code> files should contain exclusively unit tests, not integration or system tests. Integration tests should be stored in <code>*_integration_test.go</code> files and system tests in <code>*_system_test.go</code> files. <span id='unit_test_Build_Tag'></span>Optionally, unit test files may include the <code>unit_test</code> [[Go_Build_Tags#Controlling_Test_Execution_with_Build_Tags|build tag]], which allows controlling test execution based on their type (unit, functional and system). | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> |
Revision as of 17:30, 11 March 2024
External
Internal
Overview
Go comes with a lightweight test framework that includes the go test
command and the testing
package. The tests live in *_test.go
files, located in the package directory.
Packages
Test Types in Go
Unit Tests
somepkg_test.go
files should contain exclusively unit tests, not integration or system tests. Integration tests should be stored in *_integration_test.go
files and system tests in *_system_test.go
files. Optionally, unit test files may include the unit_test
build tag, which allows controlling test execution based on their type (unit, functional and system).
//go:build unit_test
package somepkg
If no tag is declared, the tests will run fine, but we lose the possibility to control when we don't want them to run. They will always run.
Also see:
Writing a Unit Test
TODO: revisit for Testify.
Write a module as shown here:
For each file containing behavior to test (a.go
)
package a
func Reverse(s string) string {
rs := []rune(s)
var result []rune
for i := len(rs) - 1; i >= 0; i-- {
result = append(result, rs[i])
}
return string(result)
}
add a <file-name>_test.go
test file. In this case a_test.go
. These files are ignored by the compiler and only compiled and executed when go test
is run. These files will be excluded from regular package builds. For more details on how go test
command handles different files, see:
The test files should be part of the same package. If that is the case, the test has access to unexpected identifiers from the package being tested. It is also possible to declare the test files into a corresponding package with the suffix _test
. In this case, the package being tested must be imported explicitly in the test. This is known as "black box" testing. Also see external test packages.
The test files should import testing
.
Add individual tests, as functions starting with TestX..
, where X...
does not start with a lowercase letter, and taking an argument t *testing.T
. The function name serves to identify the test routine. Within these functions, use Error
, Fail
or related methods to signal failure.
func TestX...(t *testing.T) {
...
t.Error("expected this: %q, got that: %q ", ...)
}
package a
import "testing"
func TestReverseEmptyString(t *testing.T) {
expected := ""
result := Reverse("")
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
func TestReverseOneCharString(t *testing.T) {
expected := "a"
result := Reverse("a")
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
func TestReverseTwoCharString(t *testing.T) {
expected := "ba"
result := Reverse("ab")
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
From the module directory, run the tests:
go test
PASS ok example.com/a 0.116s
Integration Tests
We document implementation of integration tests in go. We use this definition of integration test:
Integration tests, with or without mocks, should be placed in files named *_integration_test.go
files. The name of the file, except the ..._test.go
part that is mandated by the Go test compiler, has no influence over whether the tests contained in it are executed or not. We control the conditional execution with build tags:
//go:build integration_test
package somepkg
. └─ internal └── mypkg ├── mypkg.go ├── mypkg_test.go └── mypkg_integration_test.go
Code Coverage for Integration Tests
TO PROCESS: https://go.dev/blog/integration-test-coverage
Also see:
System Tests
We use this definition of system test:
System tests should be placed in files conventionally named *_system_test.go
files and should declare a system_test
build tag at the top of the file:
//go:build system_test
package somepkg
Benchmarks
Process this: https://pkg.go.dev/testing#hdr-Benchmarks.
Fuzzing
Process this: https://pkg.go.dev/testing#hdr-Fuzzing.
Skipping
Process this: https://pkg.go.dev/testing#hdr-Skipping.
Subtests and Sub-benchmarks
Process this: https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks.
Main
Process this: https://pkg.go.dev/testing#hdr-Main.
Mocks
Mock support: github.com/golang/mock/mockgen
TO DO
- Integrate see external test packages.
- Testing idiom "Introducing Go" page 96.
- Investigate gotest.tools module.
- Running go test immediately after cloning should succeed without any configuration or any running database. If a database is required, those are integration tests.
- https://about.sourcegraph.com/blog/go/advanced-testing-in-go