• Stars
    star
    110
  • Rank 310,765 (Top 7 %)
  • Language
    Go
  • License
    Other
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

xUnit-style test fixture adapter for go test

SMARTY DISCLAIMER: Subject to the terms of the associated license agreement, this software is freely available for your use. This software is FREE, AS IN PUPPIES, and is a gift. Enjoy your new responsibility. This means that while we may consider enhancement requests, we may or may not choose to entertain requests at our sole and absolute discretion.

Build Status Code Coverage Go Report Card GoDoc

gunit

Installation

$ go get github.com/smarty/gunit

We now present gunit, yet another testing tool for Go.

Not again... (GoConvey was crazy enough...but sort of cool, ok I'll pay attention...)

No wait, this tool has some very interesting properties. It's a mix of good things provided by the built-in testing package, the assertions you know and love from the GoConvey project, the xUnit testing style (the first real unit testing framework), and it's all glued together with go test.

Blah, blah, yeah, yeah. Ok, so what's wrong with just using the standard "testing" package? What's better about this gunit thing?

The convention established by the "testing" package and the go test tool only allows for local function scope:

func TestSomething(t *testing.T) {
	// blah blah blah
}

This limited scope makes extracting functions or structs inconvenient as state will have to be passed to such extractions or state returned from them. It can get messy to keep a test nice and short. Here's the basic idea of what the test author using gunit would implement in a *_test.go file:

package examples

import (
    "time"
	"testing"

	"github.com/smarty/assertions/should"
	"github.com/smarty/gunit"
)

func TestExampleFixture(t *testing.T) {
	gunit.Run(new(ExampleFixture), t)
}

type ExampleFixture struct {
	*gunit.Fixture // Required: Embedding this type is what makes the magic happen.

	// Declare useful state here (probably the stuff being tested, any fakes, etc...).
}

func (this *ExampleFixture) SetupStuff() {
	// This optional method will be executed before each "Test"
	// method (because it starts with "Setup").
}
func (this *ExampleFixture) TeardownStuff() {
	// This optional method will be executed after each "Test"
	// method (because it starts with "Teardown"), even if the test method panics.
}


// This is an actual test case:
func (this *ExampleFixture) TestWithAssertions() {
	// Here's how to use the functions from the `should`
	// package at github.com/smarty/assertions/should
	// to perform assertions:
	this.So(42, should.Equal, 42)
	this.So("Hello, World!", should.ContainSubstring, "World")
}

func (this *ExampleFixture) SkipTestWithNothing() {
	// Because this method's name starts with 'Skip', it will be skipped.
}

func (this *ExampleFixture) LongTestSlowOperation() {
	// Because this method's name starts with 'Long', it will be skipped if `go test` is run with the `short` flag.
	time.Sleep(time.Hour)
	this.So(true, should.BeTrue)
}

So, I see just one traditional test function and it's only one line long. What's the deal with that?

Astute observations. gunit allows the test author to use a struct as the scope for a group of related test cases, in the style of xUnit fixtures. This makes extraction of setup/teardown behavior (as well as invoking the system under test) much simpler because all state for the test can be declared as fields on a struct which embeds the Fixture type from the gunit package. All you have to do is create a Test function and pass a new instance of your fixture struct to gunit's Run function along with the *testing.T and it will run all defined Test methods along with the Setup and Teardown method.

Enjoy.

Parallelism

By default all fixtures are run in parallel as they should be independent, but if you for some reason have fixtures which need to be run sequentially, you can change the Run() method to RunSequential(), e.g. in the above example

func TestExampleFixture(t *testing.T) {
	gunit.RunSequential(new(ExampleFixture), t)
}

Advanced Examples


For users of JetBrains IDEs, here's LiveTemplate you can use for generating the scaffolding for a new fixture:

  • Abbreviation: fixture
  • Description: Generate gunit Fixture boilerplate
  • Template Text:
func Test$NAME$(t *testing.T) {
    gunit.Run(new($NAME$), t)
}

type $NAME$ struct {
    *gunit.Fixture
}

func (this *$NAME$) Setup() {
}

func (this *$NAME$) Test$END$() {
}

Be sure to specify that this LiveTemplate is applicable in Go files.

More Repositories

1

goconvey

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go
8,148
star
2

cproxy

A simple, explicit forward proxy written in Go to facilitate HTTP CONNECT Tunneling.
Go
117
star
3

assertions

Fluent assertion-style functions used by goconvey and gunit. Can also be used in any test or application.
Go
83
star
4

scanners

Variations on the bufio.Scanner interface over various types of sources (csv, strings.Fields, fixed-width, etc...).
Go
27
star
5

smartystreets-python-sdk

The official client libraries for accessing SmartyStreets APIs from Python 2.7 and 3.5
Python
27
star
6

smartystreets-php-sdk

The official client libraries for accessing SmartyStreets APIs from the PHP Hypertext Processor.
PHP
26
star
7

smartystreets-javascript-sdk

The official client libraries for accessing SmartyStreets APIs from javascript.
JavaScript
25
star
8

smartystreets-ruby-sdk

The official client libraries for accessing SmartyStreets APIs from Ruby
Ruby
23
star
9

smartystreets-dotnet-sdk

The official client libraries for accessing SmartyStreets APIs from .Net (C# and CLR-based languages)
C#
21
star
10

smartystreets-go-sdk

The official client libraries for accessing SmartyStreets APIs from Go.
Go
19
star
11

metrics

Instrumentation for golang apps
Go
15
star
12

smartystreets-java-sdk

The official client libraries for accessing SmartyStreets APIs from Java (and JRE-based languages)
Java
14
star
13

s3

Wrapped subset of AWS SDK S3 behavior
Go
7
star
14

changelog

Change logs for SmartyStreets software and data packages.
7
star
15

smartystreets-ios-sdk

The official client libraries for accessing SmartyStreets APIs from iOS
Swift
7
star
16

version-tools

POSIX-compatible shell scripts with no dependencies to facilitate calculation of semantic version values and to tag SCM (Git, etc.) repositories with a version.
Shell
6
star
17

detour

An alternate, MVC-based, approach to HTTP applications in go.
Go
4
star
18

openapi-specification

Our APIs as defined by https://openapis.org
4
star
19

smartystreets-javascript-sdk-vue-example

An example implementation of the SmartyStreets JavaScript SDK using the Vue framework.
JavaScript
4
star
20

gitreview

Script for fetching and reviewing updates to all git repositories in specified directory roots.
Go
4
star
21

sqldb

A light adapter over Go's database/sql package.
Go
3
star
22

httpstatus

Configurable HTTP status page with support for pluggable health checks.
Go
3
star
23

smartystreets-javascript-sdk-react-example

An example implementation of the SmartyStreets JavaScript SDK using the React framework.
JavaScript
3
star
24

transports

Simple wrappers around stream transport protocols, e.g. gzip and TLS
Go
1
star
25

smartystreets-javascript-sdk-angular-example

JavaScript
1
star
26

messaging-kafka

Go
1
star
27

satisfy

A simple (bare-bones) dependency manager.
Go
1
star
28

ip-filter

A project to filter out unwanted ip addresses.
Go
1
star
29

httpserver

Simple adapter of http.Server implementation to standardize interface and to facilitate clean shutdown and reload.
Go
1
star
30

shuttle

Transforms HTTP requests into intention-revealing, user instructions. After processing the given operation, it then renders the results of that operation back to the underlying HTTP response.
Go
1
star
31

smartyping

Go
1
star