testsuite

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: MIT Imports: 2 Imported by: 0

README

testsuite

Go Reference Go Report Card Coverage

testsuite provides xUnit style test suite setup and teardown behavior for golang's testing.T library missing from the Go ecosystem:

https://awesome-go.com/testing/

Features

  • SetupSuite/TeardownSuite
  • Setup/Teardown per test
  • Multiple suites per test function
  • Simple abstraction wrapping standard testing.T
  • Enforces simplicity by not allowing multiple definitions of hooks
  • Enforces simplicity by not allowing sub suites which can make it difficult to follow changes to the state of the test

Usage:

package testsuite_test

import (
    "testing"

    "github.com/berlin-ab/testsuite"
)

func TestExample(t *testing.T) {
    testsuite.New(t, "defines a test suite with setup/teardown", func(s *testsuite.S) {
        s.SetupSuite(func(t *testing.T) {
            // executes before all tests
        })

        s.Setup(func(t *testing.T) {
            // executes before each test
        })

        s.Teardown(func(t *testing.T) {
            // executes after each test
        })

        s.TeardownSuite(func(t *testing.T) {
            // executes after all tests
        })

        s.Run("it does something", func(t *testing.T) {
            // defines a test using standard *testing.T
        })
    })

    testsuite.New(t, "another suite", func(s *testsuite.S) {
        s.Run("it does something else", func(t *testing.T) {
            // defines a test using standard *testing.T
        })
    })
}

Example:


package testsuite_test

import (
    "testing"

    "github.com/berlin-ab/testsuite"
)

func TestSample(t *testing.T) {
    testsuite.New(t, "first example", func(s *testsuite.S) {
        s.SetupSuite(func(t *testing.T) {
            t.Log("setup first suite")
        })

        s.TeardownSuite(func(t *testing.T) {
            t.Log("teardown first suite")
        })

        s.Setup(func(t *testing.T) {
            t.Log("setup test")
        })

        s.Teardown(func(t *testing.T) {
            t.Log("teardown test")
        })

        s.Run("a test", func(t *testing.T) {
            t.Log("running test")
        })
    })

    testsuite.New(t, "second example", func(s *testsuite.S) {
        s.SetupSuite(func(t *testing.T) {
            t.Log("setup second suite")
        })

        s.TeardownSuite(func(t *testing.T) {
            t.Log("teardown second suite")
        })

        s.Setup(func(t *testing.T) {
            t.Log("setup test")
        })

        s.Teardown(func(t *testing.T) {
            t.Log("teardown test")
        })

        s.Run("a test", func(t *testing.T) {
            t.Log("running test")
        })
    })
}

$ go test -v ./sample_test.go | grep sample_test
sample_test.go:12: setup first suite
sample_test.go:20: setup test
sample_test.go:28: running test
sample_test.go:24: teardown test
sample_test.go:16: teardown first suite
sample_test.go:34: setup second suite
sample_test.go:42: setup test
sample_test.go:50: running test
sample_test.go:46: teardown test
sample_test.go:38: teardown second suite

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(t *testing.T, suiteName string, userProvidedSuite func(*S))

New provides a new test suite that wraps the standard *testing.T behavior with xUnit setup/teardown behavior

Types

type S

type S struct {
	// contains filtered or unexported fields
}

S holds the suite's configuration

func (*S) Run

func (s *S) Run(name string, f func(*testing.T))

Run performs a *testing.T Run behavior with the setup/teardown behavior of the testsuite

func (*S) Setup

func (s *S) Setup(f func(t *testing.T))

Setup specifies behavior that should be run before each test in the suite

note: must be specified before Run()

func (*S) SetupSuite

func (s *S) SetupSuite(f func(t *testing.T))

SetupSuite specifies behavior that should be run before running any tests in the suite

note: must be specified before Run()

func (*S) Teardown

func (s *S) Teardown(f func(t *testing.T))

Teardown specifies behavior that should be run after each test in the suite

note: must be specified before Run()

func (*S) TeardownSuite

func (s *S) TeardownSuite(f func(t *testing.T))

TeardownSuite specifies behavior that should be run after running all tests in the suite

note: must be specified before Run()

Jump to

Keyboard shortcuts

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