Golang testing: Difference between revisions

From wikinotes
(Replaced content with "Go ships with a very minimalist test library, there are also other alternatives. = Notes = <blockquote> {| class="wikitable" |- | golang std testing |- | golang tes...")
Tag: Replaced
Line 1: Line 1:
Go ships with a test suite.
Go ships with a very minimalist test library, there are also other alternatives.


= Documentation =
= Notes =
<blockquote>
<blockquote>
{| class="wikitable"
{| class="wikitable"
|-
|-
| <code>testing</code> || https://pkg.go.dev/testing@go1.18.3
| [[golang std testing]]
|-
| [[golang testify]]
|-
|-
|}
|}
</blockquote><!-- Documentation -->
</blockquote><!-- Notes -->
 
= Usage =
<blockquote>
<syntaxhighlight lang="bash">
go test -run      # run all tests
go test -run Foo  # run top-level tests containing 'Foo'
</syntaxhighlight>
</blockquote><!-- Usage -->
 
= Example =
<blockquote>
The builtin go test framework is fairly minimalist.<br>
Tests are just functions, you can loop them if useful.<br>
 
Tests are typically kept alongside code.
 
<syntaxhighlight lang="go">
// myproject/mypackage/mylib.go
 
package mypackage
 
func Hello(name string) string {
    return "Hello, " + name
}
</syntaxhighlight>
 
<syntaxhighlight lang="go">
// 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")
    }
}
</syntaxhighlight>
</blockquote><!-- Example -->
 
= Assertions =
<blockquote>
There are no assertions, you are responsible for tests and messages.
 
<syntaxhighlight lang="go">
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
}
</syntaxhighlight>
</blockquote><!-- Assertions -->
 
= Subtests =
<blockquote>
Evaluate subtests under one function using <code>t.Run("TESTNAME", ...)</code>.
 
<syntaxhighlight lang="go">
func TestHello(t *testing.T) {
    cases := []struct {                          // <-- slice of structs containing testdata
        test      string
        name      string
        expects    string
    }{ { test: "ValidName", name: "Adam", },
      { test: "NilName", name: nil, } }
 
    for _, case := range cases {
        t.Run(case.test, func(t, *testing.T) {  // <-- t.Run() evaluates each case
            res := Hello(case.name)
            if res != case.expects {
                t.Errorf("Failed because...")
            }
        }
    }
}
</syntaxhighlight>
 
You can also implement setup/teardown this way using <code>defer</code> functions.
</blockquote><!-- Subtests -->
 
= Benchmarking =
<blockquote>
There are tools for benchmarking. See docs
</blockquote><!-- Benchmarking -->
 
= Fuzzing =
<blockquote>
There are tools for fuzzing tests. See docs
</blockquote><!-- Fuzzing -->

Revision as of 00:42, 7 June 2022

Go ships with a very minimalist test library, there are also other alternatives.

Notes

golang std testing
golang testify