testchild

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 4 Imported by: 0

README

Testchild

Go Reference

Easily test subprocesses in go test.

Inspired by the subprocess testing section in GopherCon 2017: Mitchell Hashimoto - Advanced Testing with Go.

Install

go get github.com/matthewmueller/testchild

Example

package my_test

import (
	"io"
	"os"
	"os/exec"
	"strings"
	"testing"

	"github.com/matryer/is"
	"github.com/matthewmueller/testchild"
)

func TestRun(t *testing.T) {
	is := is.New(t)
	parent := func(t testing.TB, cmd *exec.Cmd) {
		cmd.Stdin = strings.NewReader("hello")
		is.NoErr(cmd.Start())
		is.NoErr(cmd.Wait())
	}
	child := func(t testing.TB) {
		is := is.New(t)
		buf, err := io.ReadAll(os.Stdin)
		is.NoErr(err)
		is.Equal(string(buf), "hello")
	}
	testchild.Run(t, parent, child)
}

Development

First, clone the repo:

git clone https://github.com/matthewmueller/testchild
cd testchild

Next, install dependencies:

go mod tidy

Finally, try running the tests:

make test

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(t testing.TB, parent func(t testing.TB, cmd *exec.Cmd), child func(t testing.TB))

Types

This section is empty.

Jump to

Keyboard shortcuts

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