Help us shape the future of Go! Take the 2026 Go Developer Survey

testing

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package testing provides generic test utilities for running test cases. This package is separate from internal/testhelper to avoid import cycles.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTestCases

func RunTestCases[T, E any](t *testing.T, tests []TestCase[T, E], testFn func(TestCase[T, E]) E)

RunTestCases runs a suite of test cases with a given test function. This eliminates duplicate for-range-t.Run patterns across test files.

Usage:

func TestMyFunction(t *testing.T) {
    tests := []testing.TestCase[InputType, string]{
        {"test name", inputValue, "expected"},
        // ... more cases
    }
    testing.RunTestCases(t, tests, func(tc TestCase[InputType, string]) string {
        return MyFunction(tc.Input)
    })
}

Type Parameters:

  • T: The input type for the test
  • E: The expected output type

Parameters:

  • t: The testing.T object
  • tests: Slice of TestCase to run
  • testFn: Function that takes a test case and returns the actual result

func RunValueTestCases

func RunValueTestCases[T, E any](
	t *testing.T,
	tests []ValueTestCase[T, E],
	testFn func(ValueTestCase[T, E]) E,
)

RunValueTestCases runs a suite of value test cases with a given test function. The test name is derived from the expected value using fmt.Sprintf. This is a lighter-weight alternative to RunTestCases for simpler tests.

Usage:

func TestMyFunction(t *testing.T) {
    tests := []testing.ValueTestCase[InputType, string]{
        {inputValue, "expected"},
        // ... more cases
    }
    testing.RunValueTestCases(t, tests, func(tc ValueTestCase[InputType, string]) string {
        return MyFunction(tc.Value)
    })
}

Type Parameters:

  • T: The input type for the test
  • E: The expected output type

Parameters:

  • t: The testing.T object
  • tests: Slice of ValueTestCase to run
  • testFn: Function that takes a test case and returns the actual result

Types

type TestCase

type TestCase[T any, E any] struct {
	Name     string
	Input    T
	Expected E
}

TestCase represents a standard test case with input and expected output. This is the common structure used across test helpers for running parameterized tests.

type ValueTestCase

type ValueTestCase[T any, E any] struct {
	Value    T
	Expected E
}

ValueTestCase represents a test case with a value and expected result. This is a simpler pattern for tests that don't need a separate name field.

Jump to

Keyboard shortcuts

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