|
|
(4 intermediate revisions by the same user not shown) |
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 fuzzing]] |
| | |- |
| | | [[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>
| |
| And you can use the following tools to evaluate tests:
| |
| | |
| <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>
| |
| | |
| <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 -->
| |