Table of Contents
Introduction
Writing tests is an essential part of software development, and Go provides a robust testing framework called “go test.” Sometimes, you may only want to run a subset of tests rather than executing all the tests within the package. In this tutorial, we will explore how to run a subset of tests in Go, allowing you to save time and focus on specific test cases.
Prerequisites
Before starting this tutorial, you should have the following prerequisites:
- Basic knowledge of Go programming language
- Go environment set up on your machine
Setting Up
To demonstrate running a subset of tests, let’s create a simple Go package with multiple test functions. Start by setting up the project structure as follows:
project/
├── main.go
└── main_test.go
In the main.go
file, add the following code to define a function that returns the sum of two numbers:
package main
func Add(a, b int) int {
return a + b
}
In the main_test.go
file, add the following code to define a few test functions:
package main
import "testing"
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, but got %d", result)
}
}
func TestSubtract(t *testing.T) {
result := Add(5, -2)
if result != 3 {
t.Errorf("Expected 3, but got %d", result)
}
}
func TestMultiply(t *testing.T) {
result := Add(4, 2)
if result != 6 {
t.Errorf("Expected 6, but got %d", result)
}
}
func TestDivide(t *testing.T) {
result := Add(10, 2)
if result != 5 {
t.Errorf("Expected 5, but got %d", result)
}
}
Running All Tests
Before diving into running a subset of tests, let’s first see how to run all the tests within a package. Open a terminal or command prompt and navigate to the root directory of the project.
To run all the tests, use the following command:
go test
You will see an output similar to the following:
PASS
ok project 0.001s
This indicates that all the tests passed successfully. If any test fails, it will be displayed in the output with an appropriate error message.
Running a Subset of Tests
To run a specific test or a subset of tests, Go provides an option called -run
. This option allows you to specify a regular expression to match against the test function names. Only the tests matching the specified pattern will be executed.
Let’s say we want to run only the tests related to addition (TestAdd
and TestSubtract
). Use the following command:
go test -run=Add
This command will execute the tests whose names contain “Add”. You will see an output similar to the following:
=== RUN TestAdd
--- PASS: TestAdd (0.000s)
=== RUN TestSubtract
--- PASS: TestSubtract (0.000s)
PASS
ok project 0.001s
Similarly, if you want to run tests related to subtraction (TestSubtract
), use the following command:
go test -run=Subtract
The output will be:
=== RUN TestSubtract
--- PASS: TestSubtract (0.000s)
PASS
ok project 0.001s
You can also combine multiple regular expressions to specify a more complex pattern. For example, to run tests related to both addition and subtraction, you can use the following command:
go test -run=Add|Subtract
This will execute both TestAdd
and TestSubtract
functions.
Conclusion
In this tutorial, you learned how to run a subset of tests in Go using the -run
option. By specifying a regular expression, you can selectively execute specific test functions based on their names. This feature allows you to save time by focusing on targeted test cases during the development process.
Now you have the knowledge to efficiently run a subset of tests in Go and optimize your testing workflow.
Happy testing!