본문 바로가기
Go

[Go] Testing

by llHoYall 2021. 10. 26.

Unit Testing

Unit testing is a software testing method by which individual units of source code.

 

This is a basic syntax for testing in Go.

import "testing"

func TestBlahBlah(t *testing.T) {
  // test statements
}
  • The test code needs to import the testing package.
  • The test function takes a parameter as "t *testing.T".
  • The test function's name has to start with Test.

Let's make a simple program to test.

 

calc.go

package calc

func Adder(a int, b int) int {
  return a + b
}

 

Now, let's make a test code.

 

calc_test.go

package calc

import "testing"

func TestAdder(t *testing.T) {
  result := Adder(1, 2)
  if result != 3 {
    t.Log("Error: should be 3, but got", result)
    t.Fail()
  }
}
  • The code that is to test and the test code should be in the same package.
  • The file name must end with _test.

If you want to test this code, execute this command.

$ go test

Then, you will see this output.

> Executing task: go test <

PASS
ok _/Users/hoya/hWorkspace/Go_Study/src 0.518s

If there is a failure, you will see this output.

--- FAIL: TestAdder (0.00s)
      calc_test.go:8: Error: should be 3, but got 4
FAIL
exit status 1
FAIL _/Users/hoya/hWorkspace/Go_Study/src 0.284s

Test a Specific Test Only

If you want to test only a specific test, use -run option.

$ go test -run <TEST_NAME>

Then, only tests containing the specified test name are performed.

Testing Using a Table

Let's modify our test code using a table.

func TestAdder(t *testing.T) {
  testTable := []struct { 
    x int
    y int
    z int
  }{
    {1, 2, 3},
    {0, 5, 5},
    {3, 4, 7},
    {4, -2, 2},
  }
  for _, elem := range testTable {
    result := Adder(elem.x, elem.y)
    if result != elem.z {
      t.Log("Expected: ", elem.z, "Got: ", result)
      t.Fail()
    } else {
      t.Log("SUCCESS")
    }
  }
}

As you can see, we can multiple tests using a table.

Coverage

You can add -cover option if you want a coverage report.

$ go test -cover

Then, the result will contain the coverage report.

 

There is a way to get the coverage report in a graphical way via HTML.

First, you need to set the cover profile.

$ go test -coverprofile=<filename>

You can choose the filename whatever you want.

Now, you can generate a graphical coverage report.

$ go test cover -html=<filename>

This is an example.

Benchmark

Go provides a benchmark as well.

  • The benchmark file name must end with _test like a unit test.
  • The benchmark code needs to import the testing package.
  • The benchmark function takes a parameter as "b *testing.B".
  • The test function's name has to start with Benchmark.
func BenchmarkAdder(b *testing.B) {
  for i := 0; i < b.N; i++ {
    Adder(1, 2)
  }
}

Now, let's run the benchmark.

$ go run -bench .

Then, all tests and benchmark functions are executed.

goos: darwin
goarch: amd64
pkg: main/src/calc
cpu: Intel(R) Core(TM) i3-8100B CPU @ 3.60GHz
BenchmarkAdder-4   1000000000   0.2789 ns/op
PASS
ok main/src/calc 0.683s

'Go' 카테고리의 다른 글

[Go] goroutine and channel  (0) 2021.10.23
[Go] Error Handling  (0) 2021.10.22
[Go] Map  (0) 2021.10.21
[Go] Linked List  (0) 2021.10.20
[Go] Functions  (0) 2021.10.19

댓글