Documentation
¶
Overview ¶
Package testscript provides integration between Go's testing package and scripttest.
The testscript package makes it easy to run scripttest tests as part of standard Go test suites, combining traditional Go tests and scripttest-based integration tests.
Basic Usage ¶
Here's a simple example:
package mypackage_test import ( "testing" "github.com/tmc/scripttestutil/testscript" ) func TestCLI(t *testing.T) { // Run all scripttest tests in the testdata directory opts := testscript.DefaultOptions() opts.Verbose = testing.Verbose() testscript.RunDir(t, "testdata", opts) }
Features ¶
The testscript package supports:
- Running individual test files or patterns - Docker-based tests - Snapshot creation and verification - Custom environment variables - Organized sub-tests for each scripttest file
Running Tests ¶
The package provides several ways to run tests:
// Run a single test file testscript.RunFile(t, "testdata/basic.txt", opts) // Run all tests in a directory testscript.RunDir(t, "testdata", opts) // Run tests matching a pattern testscript.Run(t, "testdata/cli/*.txt", opts)
Options ¶
Configure test behavior with Options:
opts := testscript.DefaultOptions() opts.Verbose = true // Enable verbose output opts.UpdateSnapshots = true // Update test snapshots opts.EnvVars = map[string]string{ // Set environment variables "DEBUG": "true", "API_URL": "http://localhost:8080", }
Example with Docker ¶
To run tests in Docker containers:
func TestInDocker(t *testing.T) { opts := testscript.DefaultOptions() opts.UseDocker = true opts.DockerImage = "golang:1.22" testscript.RunDir(t, "testdata/docker", opts) }
Parallel Testing ¶
The package works with Go's parallel testing:
func TestParallel(t *testing.T) { t.Parallel() // Enable parallel testing testscript.RunDir(t, "testdata", testscript.DefaultOptions()) }
Package testscript provides a clean connection between Go's testing framework and scripttest. It allows scripttest tests to be run as part of standard Go test suites.
Example (TestscriptWithCommands) ¶
Example_testscriptWithCommands demonstrates how to run tests with custom commands
package main import ( "fmt" ) func main() { // This won't actually run in the example, just showing the pattern fmt.Println("Running directory with custom commands:") fmt.Println(` import ( "testing" "github.com/tmc/scripttestutil/commands" "github.com/tmc/scripttestutil/testscript" "rsc.io/script" ) func TestWithExpectCommands(t *testing.T) { opts := testscript.DefaultOptions() opts.Verbose = testing.Verbose() // Register expect commands opts.SetupHook = func(cmds map[string]script.Cmd) { commands.RegisterExpect(cmds) } // Run all tests in the expect directory testscript.RunDir(t, "testdata/expect", opts) } `) }
Output: Running directory with custom commands: import ( "testing" "github.com/tmc/scripttestutil/commands" "github.com/tmc/scripttestutil/testscript" "rsc.io/script" ) func TestWithExpectCommands(t *testing.T) { opts := testscript.DefaultOptions() opts.Verbose = testing.Verbose() // Register expect commands opts.SetupHook = func(cmds map[string]script.Cmd) { commands.RegisterExpect(cmds) } // Run all tests in the expect directory testscript.RunDir(t, "testdata/expect", opts) }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Options ¶
type Options struct { // Pattern is the glob pattern to match test files (default: "testdata/*.txt") Pattern string // UseDocker determines whether to run tests in Docker containers UseDocker bool // DockerImage specifies which Docker image to use when UseDocker is true (default: golang:latest) DockerImage string // UpdateSnapshots controls whether snapshots should be updated UpdateSnapshots bool // Verbose enables verbose output Verbose bool // EnvVars defines additional environment variables to pass to tests EnvVars map[string]string // SnapshotDir specifies the directory for snapshots (default: testdata/__snapshots__) SnapshotDir string // SetupHook is a function called to set up additional commands or conditions // It receives the engine's command map which can be extended with custom commands SetupHook func(cmds map[string]script.Cmd) }
Options defines configuration options for running scripttest tests.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default test options.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner manages the execution of scripttest tests using Go's testing package.