testing

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 19 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testing provides helpers extending the standard testing package: tag-based test filtering, crypto key generation for tests, free-port allocation, and an ExampleTB for use in Example functions.

Index

Examples

Constants

View Source
const EnvTestTags = "GO_TEST_TAGS"

EnvTestTags is the environment variable Tags and SkipTags read for the comma-separated list of tags to include or, prefixed with "-", exclude.

Variables

This section is empty.

Functions

func EncodeED25519PrivateKey added in v0.2.0

func EncodeED25519PrivateKey(tb testing.TB, privateKey ed25519.PrivateKey) string

EncodeED25519PrivateKey encodes an ED25519 private key to PEM format.

func EncodeED25519PublicKey added in v0.2.0

func EncodeED25519PublicKey(tb testing.TB, publicKey ed25519.PublicKey) string

EncodeED25519PublicKey encodes an ED25519 public key to PEM format.

func EncodeRSAPrivateKey added in v0.2.0

func EncodeRSAPrivateKey(t *testing.T, privateKey *rsa.PrivateKey) string

EncodeRSAPrivateKey encodes an RSA private key to PEM format.

func EncodeRSAPublicKey added in v0.2.0

func EncodeRSAPublicKey(t *testing.T, publicKey *rsa.PublicKey) string

EncodeRSAPublicKey encodes an RSA public key to PEM format.

func FreePort added in v0.1.0

func FreePort(tb testing.TB) int

FreePort returns a free port on localhost, failing tb if none is available.

func FreePorts added in v0.11.0

func FreePorts(tb testing.TB, n int) []int

FreePorts returns n free ports on localhost, failing tb if not all are available.

func GenerateED25519KeyPair added in v0.2.0

func GenerateED25519KeyPair(tb testing.TB) (public, private string)

GenerateED25519KeyPair generates a new ED25519 key pair and returns the paths to the public and private key files.

Example usage:

func TestExample(t *testing.T) {
	publicKeyPath, privateKeyPath := GenerateED25519KeyPair(t)
	// Use the key paths for testing crypto operations
	// Files are automatically cleaned up when test completes
}

func GenerateED25519PrivateKey added in v0.2.0

func GenerateED25519PrivateKey(tb testing.TB) ed25519.PrivateKey

GenerateED25519PrivateKey generates a new ED25519 private key.

func GenerateED25519PublicKey added in v0.2.0

func GenerateED25519PublicKey(tb testing.TB, privateKey ed25519.PrivateKey, filePath string)

GenerateED25519PublicKey generates a new public key from an ED25519 private key and writes it to a file.

func GenerateRSAKeyPair added in v0.2.0

func GenerateRSAKeyPair(t *testing.T) (public, private string)

GenerateRSAKeyPair generates a new RSA key pair and returns the paths to the public and private key files.

Example usage:

func TestExample(t *testing.T) {
	publicKeyPath, privateKeyPath := GenerateRSAKeyPair(t)
	// Use the key paths for testing crypto operations
	// Files are automatically cleaned up when test completes
}

func GenerateRSAPrivateKey added in v0.2.0

func GenerateRSAPrivateKey(t *testing.T) *rsa.PrivateKey

GenerateRSAPrivateKey generates a new RSA private key.

func GenerateRSAPublicKey added in v0.2.0

func GenerateRSAPublicKey(t *testing.T, privateKey *rsa.PrivateKey, filePath string)

GenerateRSAPublicKey generates a new public key from an RSA private key and writes it to a file.

func SkipTags added in v0.0.3

func SkipTags(tags ...tagx.Tag) bool

SkipTags returns true if the tag rules apply

func Tags

func Tags(tb testing.TB, tags ...tagx.Tag)

Tags defines the tags that the test should run under.

For example:

func TestDemo(t *testing.T) {
  testing.Tags(t, tagx.Integration, tagx.Short)
}

Results being run with:

  • no tags
  • `GO_TEST_TAGS=fast`
  • `GO_TEST_TAGS=integration`
  • `GO_TEST_TAGS=fast,integration`

But would be skipped with:

  • `GO_TEST_TAGS=-fast`
  • `GO_TEST_TAGS=-integration`
  • `GO_TEST_TAGS=fast,-integration`

func WaitFor added in v0.12.0

func WaitFor(tb testing.TB, timeout time.Duration, cond func() bool)

WaitFor repeatedly invokes cond until it returns true or the timeout elapses, failing the test on timeout.

Example
package main

import (
	"fmt"
	"sync/atomic"
	"time"

	gotesting "github.com/foomo/go/testing"
)

func main() {
	tb := gotesting.NewExampleTB()

	var ready atomic.Bool

	go func() {
		time.Sleep(20 * time.Millisecond)
		ready.Store(true)
	}()

	gotesting.WaitFor(tb, time.Second, ready.Load)
	fmt.Println("ready:", ready.Load())
}
Output:
ready: true

func WaitForFreePorts added in v0.11.0

func WaitForFreePorts(tb testing.TB, ports ...int)

WaitForFreePorts blocks until each of ports becomes free on localhost, failing tb if any does not become free within 10 seconds.

Types

type ExampleTB added in v0.9.0

type ExampleTB struct {
	testing.TB // nil embed ok for examples
	// contains filtered or unexported fields
}

ExampleTB implements testing.TB for use in godoc Example functions, which receive no *testing.T. It is not concurrency-safe and does not support benchmarking; Fatal and Fatalf terminate the process via os.Exit instead of a goroutine-local exit, since examples run on the main goroutine.

func NewExampleTB added in v0.9.0

func NewExampleTB() *ExampleTB

NewExampleTB creates one for examples

Example
package main

import (
	"testing"

	gotesting "github.com/foomo/go/testing"
)

func helper(tb testing.TB) {
	tb.Helper()
	tb.Log("log called")
	tb.Logf("logf called with tb: %T", tb)
	tb.Error("error called")
	tb.Errorf("error called with tb: %T", tb)
}

func main() {
	tb := gotesting.NewExampleTB()
	helper(tb)

}
Output:
log called
logf called with tb: *testing.ExampleTB
error: error called
error: error: error called with tb: *testing.ExampleTB

func (*ExampleTB) ArtifactDir added in v0.9.0

func (t *ExampleTB) ArtifactDir() string

ArtifactDir creates and returns a new temporary directory for artifacts; it is never removed, since examples have no cleanup phase.

func (*ExampleTB) Attr added in v0.9.0

func (t *ExampleTB) Attr(_, _ string)

Attr is a no-op; examples do not report attributes.

func (*ExampleTB) Chdir added in v0.9.0

func (t *ExampleTB) Chdir(dir string)

Chdir changes the working directory for the duration of the process; it is never restored, since examples have no cleanup phase.

func (*ExampleTB) Cleanup added in v0.9.0

func (t *ExampleTB) Cleanup(_ func())

Cleanup is a no-op; examples have no cleanup phase.

func (*ExampleTB) Context added in v0.9.0

func (t *ExampleTB) Context() context.Context

Context returns context.Background().

func (*ExampleTB) Error added in v0.9.0

func (t *ExampleTB) Error(args ...any)

Error marks the example as failed and logs args to Output.

func (*ExampleTB) Errorf added in v0.9.0

func (t *ExampleTB) Errorf(format string, args ...any)

Errorf marks the example as failed and logs a formatted message to Output.

func (*ExampleTB) Fail added in v0.9.0

func (t *ExampleTB) Fail()

Fail marks the example as having failed.

func (*ExampleTB) FailNow added in v0.9.0

func (t *ExampleTB) FailNow()

FailNow marks the example as having failed. Unlike testing.T.FailNow, it does not stop execution of the current goroutine.

func (*ExampleTB) Failed added in v0.9.0

func (t *ExampleTB) Failed() bool

Failed reports whether Fail, FailNow, Error, Errorf, Fatal, or Fatalf has been called.

func (*ExampleTB) Fatal added in v0.9.0

func (t *ExampleTB) Fatal(args ...any)

Fatal marks the example as failed, logs args to Output, and calls os.Exit(1).

func (*ExampleTB) Fatalf added in v0.9.0

func (t *ExampleTB) Fatalf(format string, args ...any)

Fatalf marks the example as failed, logs a formatted message to Output, and calls os.Exit(1).

func (*ExampleTB) Helper added in v0.9.0

func (t *ExampleTB) Helper()

Helper is a no-op; examples have no call stack to mark.

func (*ExampleTB) Log added in v0.9.0

func (t *ExampleTB) Log(args ...any)

Log logs args to Output.

func (*ExampleTB) Logf added in v0.9.0

func (t *ExampleTB) Logf(format string, args ...any)

Logf logs a formatted message to Output.

func (*ExampleTB) Name added in v0.9.0

func (t *ExampleTB) Name() string

Name returns the name of the running example.

func (*ExampleTB) Output added in v0.9.0

func (t *ExampleTB) Output() io.Writer

Output returns os.Stdout, the writer used by Log, Error, and related methods.

func (*ExampleTB) Run added in v0.9.0

func (t *ExampleTB) Run(name string, f func(testing.TB)) bool

Run creates a sub-ExampleTB named t.Name()+"/"+name, runs f synchronously with it, and always returns true.

func (*ExampleTB) Setenv added in v0.9.0

func (t *ExampleTB) Setenv(key, value string)

Setenv sets an environment variable for the duration of the process; it is never restored, since examples have no cleanup phase.

func (*ExampleTB) Skip added in v0.9.0

func (t *ExampleTB) Skip(args ...any)

Skip marks the example as skipped and logs args to Output.

func (*ExampleTB) SkipNow added in v0.9.0

func (t *ExampleTB) SkipNow()

SkipNow marks the example as skipped. Unlike testing.T.SkipNow, it does not stop execution of the current goroutine.

func (*ExampleTB) Skipf added in v0.9.0

func (t *ExampleTB) Skipf(format string, args ...any)

Skipf marks the example as skipped and logs a formatted message to Output.

func (*ExampleTB) Skipped added in v0.9.0

func (t *ExampleTB) Skipped() bool

Skipped reports whether Skip, SkipNow, or Skipf has been called.

func (*ExampleTB) TempDir added in v0.9.0

func (t *ExampleTB) TempDir() string

TempDir creates and returns a new temporary directory; it is never removed, since examples have no cleanup phase.

type M added in v0.7.0

type M interface {
	Run() int
}

M is an interface representing the testing.M type, which is used for customizing the behavior of TestMain.

type MFunc added in v0.7.0

type MFunc func() int

MFunc is a wrapper type for testing.M that allows for custom behavior

func TestMain(m *testing.M) {
  goleak.VerifyTestMain(testingx.MFunc(func() int {
		return m.Run()
	}))
}

func (MFunc) Run added in v0.7.0

func (r MFunc) Run() int

Run executes the testing.M function and returns its result

Directories

Path Synopsis
Package tag provides predefined string tags for categorizing tests, consumed by github.com/foomo/go/testing.Tags and github.com/foomo/go/testing.SkipTags.
Package tag provides predefined string tags for categorizing tests, consumed by github.com/foomo/go/testing.Tags and github.com/foomo/go/testing.SkipTags.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL