Go Testing: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 18: Line 18:
==System Tests==
==System Tests==


=<span id='Writing_a_Unit_Test'></span>Write a Unit Test=
Write a module as shown here: {{Internal|Go_Modules#Declaring_Modules|Declaring a Module}}
For each file containing behavior to test (<code>a.go</code>)
<syntaxhighlight lang='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)
}
</syntaxhighlight>
add a <code><file-name>_test.go</code> test file. In this case <code>a_test.go</code>.  These files are ignored by the compiler and only compiled and executed when <code>go test</code> is run. These files will be excluded from regular package builds. For more details on how <code>go test</code> command handles different files, see: {{Internal|Go_test_Command#File_Selection|<tt>go test</tt> File Selection}}
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 <code>_test</code>. In this case, the package being tested must be imported explicitly in the test. This is known as "black box" testing. Also see [[Go_Packages#External_Test_Packages|external test packages]].
The test files should import <code>testing</code>.
Add individual tests, as functions starting with <code>TestX..</code>, where <code>X...</code> does not start with a lowercase letter, and taking an argument <code>t *testing.T</code>. The function name serves to identify the test routine. Within these functions, use <code>Error</code>, <code>Fail</code> or related methods to signal failure.
<syntaxhighlight lang='go'>
func TestX...(t *testing.T) {
  ...
  t.Error("expected this: %q, got that: %q ", ...)
}
</syntaxhighlight>
<syntaxhighlight lang='go'>
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)
}
}
</syntaxhighlight>
From the module directory, run the tests:
<syntaxhighlight lang='bash'>
go test
</syntaxhighlight>
<font size=-1>
PASS
ok  example.com/a 0.116s
</font>
=<span id='Integration_Testing'><span>Integration Tests=
=<span id='Integration_Testing'><span>Integration Tests=
{{Internal|Go_Integration_Tests#Overview|Go Integration Tests}}
{{Internal|Go_Integration_Tests#Overview|Go Integration Tests}}

Revision as of 16:56, 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

Integration Tests

System Tests

Integration Tests

Go Integration Tests

System Tests

Go System Tests

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