Test suites support for standard Go1.7 "testing" by leveraging Subtests feature

Go Suite

Build Status codecov

The support for test suites for Golang 1.7 and later.

Golang 1.7 featured Subtests that allowed you to group tests in order to share common setup and teardown logic. While that was a great addition to the testing package, it was a bit clunky syntactically. The GoSuite package leverages Golang's 1.7 Subtests feature, defines a simple TestSuite interface and runs test cases inside of them keeping setup/teardown logic for the whole suite and for single cases in place.

Quick Start

To start with, create a struct with the four methods implemented:

type MyTestSuite struct {
    // DB connection
    // etc
}

// SetUpSuite is called once before the very first test in suite runs
func (s *MyTestSuite) SetUpSuite() {
}

// TearDownSuite is called once after thevery last test in suite runs
func (s *MyTestSuite) TearDownSuite() {
}

// SetUp is called before each test method
func (s *MyTestSuite) SetUp() {
}

// TearDown is called after each test method
func (s *MyTestSuite) TearDown() {
}

Then add one or more test methods to it, prefixing them with Test prefix:

func (s *MyTestSuite) TestMyFirstTestCase(t *testing.T) {
    if !someJob {
        t.Fail("Unexpected failure!")
    }
}

Almost done! The only piece that remains is to run the suite! You do this by calling the Run method. Note, the enclosing TestIt method is a normal testing method you usually write in Go, nothing fancy at all!

func TestIt(t *testing.T) {
	Run(t, &MyTestSuite{})
}

Installation

To install Go Suite, use go get:

go get github.com/pavlo/gosuite

The import the pavlo/gosuite package into your code like this:

package yours

import (
  "testing"
  "github.com/pavlo/gosuite"
)

...

Complete Example

The complete example is shown to help you to see the whole thing on the same page. Note, it leverages the Is package for assertions... the package is great though indeed it is not required to use with Go Suite. The example however demonstrates a slick technique making the assertion methods available on the suite itself!

import (
  "testing"
  "github.com/pavlo/gosuite"
)

type Suite struct {
	*is.Is
	setUpSuiteCalledTimes    int
	tearDownSuiteCalledTimes int
	setUpCalledTimes         int
	tearDownUpCalledTimes    int
}

func (s *Suite) SetUpSuite() {
	s.setUpSuiteCalledTimes++
}

func (s *Suite) TearDownSuite() {
	s.tearDownSuiteCalledTimes++
}

func (s *Suite) SetUp() {
	s.setUpCalledTimes++
}

func (s *Suite) TearDown() {
	s.tearDownUpCalledTimes++
}

func TestIt(t *testing.T) {
    s := &Suite{Is: is.New(s.t)}
	gosuite.Run(t, s)
	
	s.Equal(1, s.setUpSuiteCalledTimes)
	s.Equal(1, s.tearDownSuiteCalledTimes)
	s.Equal(2, s.setUpCalledTimes)
	s.Equal(2, s.tearDownUpCalledTimes)
}

func (s *Suite) TestFirstTestMethod(t *testing.T) {
	s.Equal(1, s.setUpSuiteCalledTimes)
	s.Equal(0, s.tearDownSuiteCalledTimes)
	s.Equal(1, s.setUpCalledTimes)
	s.Equal(0, s.tearDownUpCalledTimes)
}

func (s *Suite) TestSecondTestMethod(t *testing.T) {
	s.Equal(1, s.setUpSuiteCalledTimes)
	s.Equal(0, s.tearDownSuiteCalledTimes)
	s.Equal(2, s.setUpCalledTimes)
	s.Equal(1, s.tearDownUpCalledTimes)
}

Running it with go test -v would emit this:

> go test -v

=== RUN   TestIt
=== RUN   TestIt/TestFirstTestMethod
=== RUN   TestIt/TestSecondTestMethod
--- PASS: TestIt (0.00s)
    --- PASS: TestIt/TestFirstTestMethod (0.00s)
    --- PASS: TestIt/TestSecondTestMethod (0.00s)
PASS
ok  	github.com/pavlo/gosuite	0.009s
Success: Tests passed.

License

Go Suite is released under the MIT License.

Similar Resources

Test case to fix a bug in github.com/golang/glog

glog Library Pollutes Global Flags List This problem has been reported quite a few times, over the years; see it reported at golang-nuts mailing list,

Feb 10, 2022

Litter is a pretty printer library for Go data structures to aid in debugging and testing.

Litter Litter is a pretty printer library for Go data structures to aid in debugging and testing. Litter is provided by Sanity: The Headless CMS Const

Dec 28, 2022

Log-generator - A simple CLI tool that generates near real logs for testing

Log-generator - A simple CLI tool that generates near real logs for testing

Jan 22, 2022

Tracetest - Trace-based testing. End-to-end tests powered by your OpenTelemetry Traces.

Tracetest - Trace-based testing. End-to-end tests powered by your OpenTelemetry Traces.

End-to-end tests powered by OpenTelemetry. For QA, Dev, & Ops. Live Demo | Documentation | Twitter | Discord | Blog Click on the image or this link to

Jan 3, 2023

Drop-in replacement for Go's stringer tool with support for bitflag sets.

stringer This program is a drop-in replacement for Go's commonly used stringer tool. In addition to generating String() string implementations for ind

Nov 26, 2021

Peimports - based on golang's debug/pe this package gives quick access to the ordered imports of pe files with ordinal support

This code is almost entirely derived from the Go standard library's debug/pe package. It didn't provide access to ordinal based entries in the IAT and

Jan 5, 2022

Tlog - Golang log but via telegram bot support

tlog golang log but via telegram bot support how to use tlog.LinkBot("token", "c

Nov 25, 2022

Golog is a logger which support tracing and other custom behaviors out of the box. Blazing fast and simple to use.

GOLOG Golog is an opinionated Go logger with simple APIs and configurable behavior. Why another logger? Golog is designed to address mainly two issues

Oct 2, 2022

Simple & efficient Go library for getting daily foreign exchange rates. Built-in support for 50+ currencies.

go-forex Simple and efficient Go library for getting daily foreign exchange rates. Built-in support for ca. 50 currencies. Also includes a simple comm

Sep 12, 2022
Comments
  • Setup and TearDown methods don't have access to testing object

    Setup and TearDown methods don't have access to testing object

    Hi,

    I like the simplicity of this package and the fact that it stays close to the standard library.

    However, is there any reason for not passing the testing object to the setup/teardown methods? E.g.:

    type TestSuite interface {
    	SetUpSuite(t *testing.T)
    	TearDownSuite(t *testing.T)
    	SetUp(t *testing.T)
    	TearDown(t *testing.T)
    }
    

    Otherwise a I don't see a way for a test setup to fail cleanly (think integration tests which for example try to connect to a database during setup).

    Alternatively, or even additionally, the above methods could return an error, and gosuite.Run would fail the test if not nil.

    Thanks

A feature-rich and easy to use logger for golang
A feature-rich and easy to use logger for golang

A feature-rich and easy to use logger for golang ?? Install ?? Common Logs lumber.Success() lumber.Info() lumber.Debug() lumber.Warning()

Dec 31, 2022
🪵 A dead simple, pretty, and feature-rich logger for golang
🪵 A dead simple, pretty, and feature-rich logger for golang

?? lumber ?? A dead simple, pretty, and feature-rich logger for golang ?? Install ?? Logging Functions lumber.Success() lumber.Info() lumber.Debug() l

Jul 20, 2022
Clean architecture starter pack for faster and easier feature creation using Go.
Clean architecture starter pack for faster and easier feature creation using Go.

cleanarchGo ?? About Clean architecture starter pack for faster and easier feature creation using Go. This pack includes Clean Architecture with the E

May 8, 2023
An golang log lib, supports tracking and level, wrap by standard log lib

Logex An golang log lib, supports tracing and level, wrap by standard log lib How To Get shell go get gopkg.in/logex.v1 source code import "gopkg.in/

Nov 27, 2022
This package enables json output, level logging and so on to standard go logger.

logplug This package enables json output, level logging and so on to standard logger. Usage log.SetOutput(logplug.NewJSONPlug(os.Stderr, logplug.LogF

Dec 27, 2021
Go-metalog - Standard API for structured logging

Metalog is a standard API for structured logging and adapters for its implementa

Jan 20, 2022
Monitor pipe progress via output to standard error.

Pipe Monitor Monitor pipe progress via output to standard error. Similar to functionality provided by the Pipe Viewer (pv) command, except this comman

Nov 14, 2022
Gin adapter for standard net/http middleware

midgin An adapter to use standard net/http middleware in Gin. Overview Gin is a very capable web framework, but it does not directly support standard

Feb 12, 2022
a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library

English | 中文 olog olog is a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library. Support outpu

Apr 12, 2023
Used to test the log collection function.

Used to test the log collection function.

Nov 30, 2021