Like VonC said, you can use +build
tags
┌─ oneofone@Oa [/t/tst-tag]
└──➜ ls
a_test.go b_test.go c_test.go
a_test.go :
package tags
import "testing"
func TestA(t *testing.T) {}
b_test.go :
// +build !feature1
package tags
import "testing"
func TestB(t *testing.T) {}
c_test.go :
// +build !feature1
// +build !feature2
package tags
import "testing"
func TestC(t *testing.T) {}
Then run the test with the -tags
parameter :
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags feature1 . | grep PASS:
--- PASS: TestA (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags feature2 . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
// Update : different logic :
a_test.go:
// +build all
package tags
import "testing"
func TestA(t *testing.T) {}
b_test.go:
// +build all feature1
package tags
import "testing"
func TestB(t *testing.T) {}
c_test.go:
// +build all feature2
package tags
import "testing"
func TestC(t *testing.T) {}
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags all | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags feature1 | grep PASS:
--- PASS: TestB (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags="feature1 feature2" | grep PASS:
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
Or you call specific tests by name like :
d_test.go:
package tags
import "testing"
func TestA1(t *testing.T) {}
func TestB1(t *testing.T) {}
func TestC1(t *testing.T) {}
func TestD1(t *testing.T) {}
Output:
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -run="(A|B)1" -v | grep PASS:
--- PASS: TestA1 (0.00 seconds)
--- PASS: TestB1 (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -run="D1" -v | grep PASS:
--- PASS: TestD1 (0.00 seconds)