How to use the command `go test` (with examples)
The go test
command is used to test Go packages. It is a powerful tool for running tests and benchmarks on Go code. This article will illustrate several use cases of the go test
command.
Use case 1: Test the package found in the current directory
Code:
go test
Motivation:
Running go test
in the current directory allows you to test the package located in that directory. This is useful when you want to quickly run tests and check if there are any test failures.
Explanation:
The go test
command without any arguments tests the package in the current directory. It searches for files ending with _test.go
and executes the associated tests.
Example output:
ok example.com/mypackage 0.012s
Use case 2: Verbosely test the package in the current directory
Code:
go test -v
Motivation:
Adding the -v
flag to the go test
command enables verbose output. This can be helpful when you want to see detailed information about each test case being executed, including its status and any log messages that are printed during the test.
Explanation:
The -v
flag stands for “verbose” and it enables verbose output. When used with the go test
command, it prints out detailed information about each test case, including the test names, their status (pass or fail), and any log messages that are printed during the test.
Example output:
=== RUN TestFoo
--- PASS: TestFoo (0.005s)
=== RUN TestBar
--- FAIL: TestBar (0.021s)
example_test.go:12: TestBar failed
FAIL example.com/mypackage 0.027s
Use case 3: Test the packages in the current directory and all subdirectories
Code:
go test -v ./...
Motivation:
Using ./...
as the package argument allows you to test all the packages in the current directory and its subdirectories. This is useful when you have multiple packages in your project and want to test them all at once.
Explanation:
The ./...
package argument is a wildcard that tells the go test
command to test all packages in the current directory and its subdirectories. This is useful when you have a project with multiple packages and want to run tests for all of them.
Example output:
ok example.com/mypackage 0.012s
ok example.com/mypackage/subpackage 0.023s
Use case 4: Test the package in the current directory and run all benchmarks
Code:
go test -v -bench .
Motivation:
Running benchmarks along with tests allows you to measure the performance of your code. This is useful when you want to identify performance bottlenecks and optimize your algorithms or data structures.
Explanation:
The -bench
flag followed by .
runs all benchmarks in the package. Benchmarks are special types of tests that measure the performance of code snippets. Running benchmarks can help you identify slow parts of your code and optimize them.
Example output:
BenchmarkFoo-8 10000000 112 ns/op
BenchmarkBar-8 5000000 220 ns/op
PASS
ok example.com/mypackage 3.981s
Use case 5: Test the package in the current directory and run all benchmarks for 50 seconds
Code:
go test -v -bench . -benchtime 50s
Motivation:
Increasing the benchmark time allows you to collect more accurate performance measurements. This is useful when you want to get a better understanding of the performance characteristics of your code.
Explanation:
The -benchtime
flag followed by a duration allows you to set the benchmarking time. In this example, we set the benchmark time to 50 seconds. This allows the benchmarks to run for a longer duration, which can lead to more reliable and accurate results.
Example output:
BenchmarkFoo-8 100000000 11.2 ns/op
BenchmarkBar-8 50000000 22.0 ns/op
PASS
ok example.com/mypackage 1.981s
Use case 6: Test the package with coverage analysis
Code:
go test -cover
Motivation:
Analyzing code coverage helps you understand how well your tests are exercising your code. This is useful when you want to identify untested parts of your code and write additional tests to increase coverage.
Explanation:
The -cover
flag enables coverage analysis. It measures the coverage of your tests, indicating which parts of your code are being exercised and which parts are not. By running go test
with the -cover
flag, you can get insights into the coverage of your tests.
Example output:
coverage: 84.3% of statements
ok example.com/mypackage 1.432s
Conclusion:
The go test
command is a powerful tool for testing Go packages. It allows you to test individual packages, run benchmarks, analyze coverage, and more. By understanding the different use cases of the command and its various flags, you can effectively test and improve the quality of your Go code.