GoLang Testify Examples
A crucial component of Go programming is Golang testing. The Go testing framework, provided by the standard library’s testing package, offers a robust and convenient way to write tests for Go code.
Testing ensures that the code you write works as intended and generates the desired results. It enables you to catch bugs and identify the issues early in the development process. Test functions serve as executable documentation. They provide concrete examples on how to use your code and illustrate the expected behavior of functions, methods, and packages.
Overall, Golang testing is crucial to maintain the code quality, ensure correctness, and enable a collaboration among developers. Using the test-driven development (TDD) methodology and writing thorough tests, you can build a reliable and robust software in Go.
This post demonstrates the two examples to test some functions in Go.
Example 1: Testing a Simple Function
Testing a simple function that refers to the process of writing tests specifically for a standalone function in Go. When testing a simple function, the objective is to verify that the function behaves as expected and produces the correct output for various inputs and scenarios.
A complete example of a simple test in Go with detailed explanations is provided in the following:
In this code, we have a simple test for the “Add” function. Let’s comprehend the code:
In order to identify that this file is a part of the main package, the code begins with the package declaration, the “package main”. After that, we import the testing package which provides support for writing tests in Go.
The “Add” function is defined. It produces the sum of two numbers that is stored in variables “a” and “b”. The “TestAdd” function is the test function for the “Add” function. In Go, the test functions must start with the word “Test” and have a single parameter of type *testing.T. This parameter is used to report the errors and control the test execution.
Inside the “TestAdd”, we define three test cases. For Test Case 1, we invoke the “Add” method with the numbers 2 and 3 and assign the result to the “result” variable. We set the expected variable to 5 since we expect the sum of 2 and 3 to be 5. Then, we utilize an “if” statement to evaluate if the output matches what is expected. If not, an error is sent with a message using the “t.Errorf”.
For Test Case 2, we invoke the “Add” function with the values of 10 and -5, and perform the same check as in Test Case 1. For Test Case 3, we call the “Add” function with the values of 0 and 0, and perform the same check as in Test Case 1.
The main function is defined to run the tests. We call the “testing.Main” with “nil” arguments to setup the testing environment and execute the tests that are defined in the package.
When you run this code using the Go “test” command, the “TestAdd” function is executed. Each test case is run separately. If any of the checks in the “if” statements fails, an error is reported.
The expected output when running the tests and all cases that pass successfully is as follows:
The “PASS” message indicates that all test cases within the “TestAdd” function is passed without any error. The tests are successfully completed as shown by the exit status of 0.
If any test case fails, an error message is printed which indicates the expected and actual values, and the exit status is non-zero.
Example 2: Testing a Struct Method
Testing a struct method that refers to the process of writing tests specifically for a method that is associated with a struct in Go. In Go, a struct is a user-defined composite data type that can encapsulate the data fields and methods.
This code demonstrates a test for the “Area” method of a “Rectangle” struct:
The code starts with the package declaration which is “package main”. We import the “testing” package. Rectangle is a specified struct that represents a rectangle that has width and height variables.
The “Rectangle” struct has a defined “Area” function. By multiplying the rectangle’s width and height, it estimates and yields the area of the rectangle. The “TestRectangleArea” function is the test function for the “Area” method. Inside the “TestRectangleArea”, we perform these steps: Create an instance of rectangle with a width of 3 and a height of 4 using the struct literal syntax. Invoke the rectangle instance’s “Area” function to calculate the area and assign it to the “result” variable. Set the expected variable to 12 since we expect the area of a rectangle with a width of 3 and a height of 4 to be 12.
To evaluate the outcome with the predicted value, use an “if” statement. If they do not match, we use the “Errorf” to indicate trouble with a message.
The main function is defined to run the tests. We call the “testing.Main” with “nil” arguments.
When you run this code using the Go test command, the “TestRectangleArea” function is executed. The test case creates a rectangle instance, calculates its area using the “Area” method, and compares the result with the expected value. An error is displayed if the check fails.
The expected output when running the tests and the test case that passes successfully is as follows:
The “PASS” message indicates that the test case within the “TestRectangleArea” function is passed without any error.
To check if the provided code is working correctly and producing the right output, Golang testing is carried out. This article provided two illustrations of the Golang test examples. The first illustration conducted a simple test in Go in which we created three test cases to add some numbers. Whereas for the second illustration, we calculated the area of a rectangle using the Golang testing package.