The Go language is an excellent programming language known for its efficiency, simplicity and powerful standard library. When we develop applications using Go, writing and running test code is an integral part of it. This article explains how to use the Go language for testing to ensure that our code works as expected.
In the Go language, test code is equivalent to ordinary code. Therefore, we can write and run test codes to ensure that they run our application correctly. Typically, test code should be placed in a separate directory for easier management and maintenance.
The following is a simple example:
package main import "testing" func TestAdd(t *testing.T) { result := add(2, 3) expected := 5 if result != expected { t.Errorf("add(2, 3) = %d; expected %d", result, expected) } } func add(a, b int) int { return a + b }
In the above example, we wrote a test function named "TestAdd", which tests the add() function we wrote ourselves function. Testing functions usually start with "Test" and require a *testing.T parameter named "t", which is used to report test results.
In the test function, we first call the add() function and store its result in the result variable. Next, we store the expected result in the expected variable and use an if statement to compare the result to the expected result. If the result is not as expected, we will report an error using the t.Errorf() function.
Once we have written the test code, we need to run it to ensure that our code works correctly. In Go language, test code can be easily run using the go test command.
Switch to the directory where the test code is stored in the terminal and enter the following command:
go test
This command will automatically detect the test codes in the directory and run them. If all tests pass, the following message will be printed:
ok <package_name> 0.500s
Otherwise, it will print which tests failed and return an appropriate error message.
In addition to unit testing, we also need to conduct benchmarking to determine how our code performs under different amounts of input. In Go language, we can use the Benchmark function in the testing package to write benchmark code.
The following is an example:
package main import "testing" func BenchmarkAdd(b *testing.B) { for i := 0; i < b.N; i++ { add(2, 3) } } func add(a, b int) int { return a + b }
In the above example, we wrote a benchmark function named "BenchmarkAdd", which tests the add() function we wrote ourselves Performance. Benchmark functions usually start with "Benchmark" and require a *testing.B parameter named "b".
In the benchmark function, we use a for loop to run the add() function b.N times, where b.N is an integer representing how many times the function should be run.
Similar to running unit tests, we can easily run benchmarks using the go test command. In the terminal, switch to the directory where the benchmark code is stored and enter the following command:
go test -bench=.
This command will automatically detect the benchmark code in the directory and run them. If all tests pass, the following message will be output:
BenchmarkAdd-4 10000 174060 ns/op
Where, "BenchmarkAdd-4" refers to the name of the benchmark function and the number of GOMAXPROCS, and "10000" refers to the number of runs of the add() function. The number of times, "174060 ns/op" refers to the average time consumed each time the add() function is run.
Conclusion
In this article, we introduced how to use Go language for testing. We first learned how to write unit test code to ensure that our code works correctly. Next, we learned how to write benchmark code to determine how our code performs with different amounts of input.
In general, testing is an integral part of Go programming. With good testing, we can improve and modify our code with greater confidence, resulting in more reliable applications.
The above is the detailed content of How to test using Go language?. For more information, please follow other related articles on the PHP Chinese website!