Tests in Go
Explore how to use Go's testing features such as subtests and table-driven tests to write clean and maintainable test code. Understand various testing options including coverage reporting and concurrency detection to improve test reliability and organization.
We'll cover the following...
We'll cover the following...
Go has some fantastic features for tests that are worth mentioning.
Subtests
Subtests help us write more structured test code. Let’s use an example to understand this:
For the rest of the lesson, we’ll rely on this production function for writing tests. The NewPerson function has two branches to cover:
- The happy one is when we provide a valid
age. - The bad one is when we pass an invalid
age.
With that being said, we have to write two test functions that we’ll call TestNewPerson_WithValidAge and TestNewPerson_WithNotValidAge ...