# Unit Test in Go <!-- Put the link to this slide here so people can follow --> slide: [@eddielin0926/unit-test-in-go](https://hackmd.io/@eddielin0926/unit-test-in-go) --- ## Why do we need testing? - Improve the quality of your product - Enhancing the development process - Saving your time and your money --- ### Different Types of Tests ![](https://i.imgur.com/9NHNEYj.png) --- ### Test-driven Development ![](https://i.imgur.com/YC5y9UU.png) --- ## Unit Test Test your code! --- ### Creating a Test - \<package\>_test.go - In same package folder - `func TestXxx(*testing.T)` ---- #### Structure ```shell ❯ tree go-unit-test go-unit-test ├── go.mod └── math ├── math.go └── math_test.go 1 directory, 3 files ``` ---- #### math.go ```go package math func Add(x, y int) int { return x + y } func Subtract(x, y int) int { return x - y } func Divide(x, y int) float64 { return float64(x / y) } func Multiply(x, y int) int { return x * y } ``` --- ### math_test.go ```go package math import "testing" func TestAdd(t *testing.T) { result := Add(1, 3) if result != 4 { t.Errorf("Add(1, 3) FAILED. Expected %d, got %d\n", 4, result) } else { t.Logf("Add(1, 3) PASSED. Expected %d, got %d\n", 4, result) } } ``` --- ### Run the Test ```shell ❯ go test PASS ok go-unit-test/math 0.001s ❯ go test -v === RUN TestAdd math_test.go:11: Add(1, 3) PASSED. Expected 4, got 4 --- PASS: TestAdd (0.00s) PASS ok go-unit-test/math 0.001s ❯ go test --cover PASS coverage: 25.0% of statements ok go-unit-test/math 0.001s ``` --- ### Failure Case ```Go func TestDivide(t *testing.T) { result := Divide(1, 2) if result != 0.5 { t.Errorf("Divide(1, 2) FAILED. Expected %f, got %f\n", 0.5, result) } else { t.Logf("Divide(1, 2) PASSED. Expected %f, got %f\n", 0.5, result) } } ``` --- ### Run the Failure Test ```Go ❯ go test -v === RUN TestAdd math_test.go:11: Add(1, 3) PASSED. Expected 4, got 4 --- PASS: TestAdd (0.00s) === RUN TestDivide math_test.go:19: Divide(1, 2) FAILED. Expected 0.500000, got 0.000000 --- FAIL: TestDivide (0.00s) FAIL exit status 1 FAIL go-unit-test/math 0.001s ``` --- ## The AAA Pattern - Arrange - Act - Assert --- ### Install assert ```shell ❯ go get github.com/stretchr/testify/assert ``` ```Go import ( "testing" "github.com/stretchr/testify/assert" ) ``` --- ### Try it! ```Go func TestSub(t *testing.T) { // Arrange var a int = 13 var b int = 9 var expected int = 4 // Act var actual int = Subtract(a, b) // Assert assert.EqualValues(t, expected, actual, "Expected %d, got %f\n", expected, actual) } ``` --- ### Result ```Go ❯ go test -v --cover === RUN TestAdd math_test.go:15: Add(1, 3) PASSED. Expected 4, got 4 --- PASS: TestAdd (0.00s) === RUN TestDivide math_test.go:23: Divide(1, 2) FAILED. Expected 0.500000, got 0.000000 --- FAIL: TestDivide (0.00s) === RUN TestSub --- PASS: TestSub (0.00s) FAIL coverage: 75.0% of statements exit status 1 FAIL go-unit-test/math 0.002s ``` --- ## Ginkgo ```go= var _ = Describe("Books", func() { var book *books.Book BeforeEach(func() { book = &books.Book{ Title: "Les Miserables", Author: "Victor Hugo", Pages: 2783, } Expect(book.IsValid()).To(BeTrue()) }) It("can extract the author's last name", func() { Expect(book.AuthorLastName()).To(Equal("Hugo")) }) }) ``` --- ### Reference - [GoLang Unit Testing and Mock Testing Tutorial](https://www.youtube.com/watch?v=XQzTUa9LPU8) - [Go testing](https://pkg.go.dev/testing) - [Go assert](https://pkg.go.dev/github.com/stretchr/testify/assert)
{"title":"Unit Test","breaks":true,"metaMigratedAt":"2023-06-17T16:43:33.944Z","metaMigratedFrom":"YAML","description":"Writing unit test in golang.","contributors":"[{\"id\":\"7a5817e4-39bc-468d-bb85-1ad0c6fea03d\",\"add\":6238,\"del\":2438}]"}
    555 views