Golang test

From 탱이의 잡동사니
Revision as of 11:43, 8 April 2020 by Pchero (talk | contribs) (→‎Basic)
Jump to navigation Jump to search

Overview

golang test 내용정리.

Basic

Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the "go test" command, which automates execution of any function of the form

func TestXxx(*testing.T)

where Xxx does not start with a lowercase letter. The function name serves to identify the test routine.

With these functions, use the Error, Fail or related methods to signal failure.

To write a new test suite, create a file whose name ends _test.go that contains the TestXxx functions are described here. Put the file in the same package as the one being tested. The file will be excluded from regular package builds but will be included when the "go test" command is run. For more detail, run "go help test" and "go help testing".

A simple test function looks like this:

fund TestAbs(t *testing.T) {
  got := Abs(-1)
  if got != 1 {
    t.Errorf("Abs(-1) = %d; want 1", got)
  }
}

Testing flags

Benchmarks

Functions of the form

fund BenchmarkXxx(*testing.B)

are considered benchmarks, and are executed by the "go test" command when its -bench flag is provided. Benchmarks are run sequenctially.

A sample benchmark function looks like this <source lang=go> fund BenchmarkHello(b * testing.B) { } </source>

TestMain


See also