testing

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 18 Imported by: 3

Documentation

Index

Examples

Constants

View Source
const EnvTestTags = "GO_TEST_TAGS"

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

func FreePorts added in v0.11.0

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

FreePorts returns a free ports on localhost

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 WaitForFreePorts added in v0.11.0

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

WaitForFreePorts returns a free port on localhost

Types

type ExampleTB added in v0.9.0

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

func NewExampleTB added in v0.9.0

func NewExampleTB() *ExampleTB

NewExampleTB creates one for examples

Example
package main

import (
	"testing"

	testingx "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 := testingx.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

func (*ExampleTB) Attr added in v0.9.0

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

func (*ExampleTB) Chdir added in v0.9.0

func (t *ExampleTB) Chdir(dir string)

func (*ExampleTB) Cleanup added in v0.9.0

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

func (*ExampleTB) Context added in v0.9.0

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

func (*ExampleTB) Error added in v0.9.0

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

func (*ExampleTB) Errorf added in v0.9.0

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

func (*ExampleTB) Fail added in v0.9.0

func (t *ExampleTB) Fail()

func (*ExampleTB) FailNow added in v0.9.0

func (t *ExampleTB) FailNow()

func (*ExampleTB) Failed added in v0.9.0

func (t *ExampleTB) Failed() bool

func (*ExampleTB) Fatal added in v0.9.0

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

func (*ExampleTB) Fatalf added in v0.9.0

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

func (*ExampleTB) Helper added in v0.9.0

func (t *ExampleTB) Helper()

func (*ExampleTB) Log added in v0.9.0

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

func (*ExampleTB) Logf added in v0.9.0

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

func (*ExampleTB) Name added in v0.9.0

func (t *ExampleTB) Name() string

func (*ExampleTB) Output added in v0.9.0

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

func (*ExampleTB) Run added in v0.9.0

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

func (*ExampleTB) Setenv added in v0.9.0

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

func (*ExampleTB) Skip added in v0.9.0

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

func (*ExampleTB) SkipNow added in v0.9.0

func (t *ExampleTB) SkipNow()

func (*ExampleTB) Skipf added in v0.9.0

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

func (*ExampleTB) Skipped added in v0.9.0

func (t *ExampleTB) Skipped() bool

func (*ExampleTB) TempDir added in v0.9.0

func (t *ExampleTB) TempDir() string

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

Jump to

Keyboard shortcuts

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