Golang testing: Difference between revisions

From wikinotes
(Created page with "Go ships with a test suite. = Example = <blockquote> {{ TODO | finish }} <syntaxhighlight lang="go"> package mypackage func Hello(name string) string { return "Hello, "...")
 
No edit summary
Line 1: Line 1:
Go ships with a test suite.
Go ships with a test suite.
= Documentation =
<blockquote>
{| class="wikitable"
|-
| <code>testing</code> || https://pkg.go.dev/testing@go1.18.3
|-
|}
</blockquote><!-- Documentation -->
= 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 =
= Example =
<blockquote>
<blockquote>
{{ TODO |
The builtin go test framework is fairly minimalist.<br>
finish }}
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">
<syntaxhighlight lang="go">
// myproject/mypackage/mylib.go
package mypackage
package mypackage


Line 16: Line 49:
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// myproject/mypackage/mylib_test.go
// myproject/mypackage/mylib_test.go
package mypackage
package mypackage


import "testing"
import "testing"


func TestHello(t *testing.T) {
    res := Hello("Adam")
    if res != "Hello, Adam" {
        t.Errorf("Hello() result did not match")
    }
}
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Example -->
</blockquote><!-- Example -->

Revision as of 21:51, 6 June 2022

Go ships with a 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,
And you can use the following tools to evaluate tests:

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
}
// 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")
    }
}