Go Test with Testify: Most Simple Way to Implement Unit Test
Tags: unittest, development, go, tutorial
Published: 2024-05-05 01:24:11

### Preamble
Unit test is testing specific method using several test cases, that performed by the developer who create the method, to prove the method running as expected.

---

### Preparation
Let’s say we have a method that generate OTP code that we can specify the length of the generated OTP code, and we want to test it, in this story I’ll using testify for assertion toolkit. Please take a look this library [Testify Go](https://github.com/stretchr/testify?).

---

### Coding Time

#### OTP Generator Code
```go
package generator

import (
	"crypto/rand"
	"errors"
	"fmt"
)

func OTP(length int) (string, error) {
	const otpChars = "1234567890"
	if length < 1 {
		return "", errors.New("can't generate code with less than 1")
	}
	buffer := make([]byte, length)
	_, err := rand.Read(buffer)
	if err != nil {
		return "", fmt.Errorf("OTP generator error : %+v", err)
	}
	otpCharsLength := len(otpChars)
	for i := 0; i < length; i++ {
		buffer[i] = otpChars[int(buffer[i])%otpCharsLength]
	}
	return string(buffer), nil
}
```
#### Let's Test it!
In golang test file should be inside the same package of the function which will be tested, for better understand your OTP generator method is inside otp.go and the test file should be otp_test.go, here is the sample structure of file.
![File Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vydtk2ufy7p8ot8c9mpc.png)

#### Unit Testing Code for OTP Generator Code
```go
package generator

import (
	"github.com/stretchr/testify/suite"
	"testing"
)

// otpTestSuite embedding suite.Suite of testify
type otpTestSuite struct {
	suite.Suite
}

// TestOTPTestSuite initialize test suite
func TestOTPTestSuite(t *testing.T) {
	suite.Run(t, new(otpTestSuite))
}

// TestOTP testing OTP() function using several test cases
func (s *otpTestSuite) TestOTP() {

	// make test cases
	tests := []struct {
		TestCaseName string
		Length       int
		WantError    bool
	}{
		{
			TestCaseName: "Test case generate otp with 6 digit and no error",
			Length:       6,
			WantError:    false,
		},
		{
			TestCaseName: "Test case generate otp with 12 digit and no error",
			Length:       12,
			WantError:    false,
		},
		{
			TestCaseName: "Test case generate otp with 0 digit and error",
			Length:       0,
			WantError:    true,
		},
	}

	// scan test cases
	for _, test := range tests {
		s.Run(test.TestCaseName, func() {
			// start test based on test case
			otp, err := OTP(test.Length)
			if test.WantError {
				s.Assert().Error(err)
			} else {
				s.Assert().NoError(err)
			}
			s.Assert().Len(otp, test.Length)
		})
	}

}
```

---

### Run Through The Go Unit Test Files.
To run go test recursively change your current directory to root of project, then you can runing this command.
```bash
go test -v ./...
```
And the result should look like this.
![Running through all Unit Test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0dd0tqc2lsywdrvqnih.png)

---

### Run Through The Go Unit Test Files with Coverage Details.
Still in side the root directory of your project. You can run this command.
```bash
go test ./... -coverprofile cover.out
go tool cover -html=cover.out
```
And automatically open browser and show the detail coverage of the OTP code statements.
![Running through all Unit Test with Detail Coverage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/triopvwrbi6hudnaz1ek.png)

---

### Conclusion
So, to sum it up, unit testing in Golang is like having a trusty sidekick for your code. With tools like Testify, you can easily check if your functions are doing what they're supposed to, running tests and checking coverage is as simple as a couple of commands.

---

### My Thanks
Thank you for visiting! This article was originally published on another website. You can find the original source [here](https://0marumaru.medium.com/unit-test-best-practice-in-golang-e17589279909). I'm excited to bring it over here. I hope you found it useful and enjoyable. Don't hesitate to reach out if you have any questions or feedback. Happy reading!