Giter Site home page Giter Site logo

ensure's Introduction

Ensure

A scenario-based test runner for Go

Installation

go get github.com/iamkoch/ensure

Basic scenario

package myapp 

import (
    "testing"
    "github.com/iamkoch/ensure"
)

func TestExample(t *testing.T) {
    var aThing bool

    ensure.That("Testing a thing", func(s *ensure.Scenario) {
        s.Given("a thing is false", func() {
            aThing = false
        })
		
        s.When("I set a thing to true", func() {
            aThing = true
        })
		
        s.Then("the thing should be true", func() {
            if !aThing {
                t.Error("aThing should be true")
            }
        })
    }, t)
}

Background, And, Teardown

This library also supports Background, And, and Teardown steps:

package myapp

import (
	"testing"
	"context"
	"github.com/iamkoch/ensure"
)

func TestExample(t *testing.T) {
    var aThing bool

    ensure.That("Testing a thing", func(s *ensure.Scenario) {
        s.Background("Prepare the scenario", func() {
            // Do something here
        })
        
        s.Given("a thing is false", func() {
            aThing = false
        })
		
        s.And("another thing happens", func() {
            // Do another thing here
        })
		
        s.When("I set a thing to true", func() {
            aThing = true
        })
        
        s.Then("the thing should be true", func() {
            if !aThing {
                t.Error("aThing should be true")
            }
        }).Teardown("revert a thing", context.Background(), func(ctx context.Context) {
            aThing = false
        })
    }, t)
}

Printing

Scenario text is, by default, printed to stdout using fmt.Println. This can be overridden by setting the Printer variable to a custom implementation of the Printer interface.

This can be overridden by setting the Printer variable to a custom implementation of the Printer interface.

package myapp

type testPrinter struct {
	entries []string
}

func (t *testPrinter) Write(s string) {
	t.entries = append(t.entries, s)
}

func (t *testPrinter) Output() string {
	var output string
	for _, entry := range t.entries {
		output += entry + "\n"
	}
	return output
}

func init() {
	ensure.Printer = &testPrinter{}
}

func TestMyThing(t *testing.T) {
	...
}

Full Example

This is taken from our own test in lib_test.go:

func TestFullScenario(t *testing.T) {
	var (
		testCtx      = context.Background()
		aThing       bool
		anotherThing bool
		tornDown     = false
	)

	That("A full scenario runs as expected", func(s *Scenario) {
		s.Given("a thing is false", func() {
			aThing = false
		})

		s.And("another thing is true", func() {
			anotherThing = true
		}).Teardown("revert anotherThing", testCtx, func(ctx context.Context) {
			anotherThing = false
		})

		s.When("I do the old swaperoo", func() {
			aThing = true
			anotherThing = false
		})

		s.Then("the a thing should be true", func() {
			require.Equal(t, true, aThing)
		})

		s.And("anotherThing should be false", func() {
			require.Equal(t, false, anotherThing)
		}).Teardown("tearDown", testCtx, func(ctx context.Context) {
			tornDown = true
		})
	}, t)

	require.True(t, tornDown)

	require.Equal(t, `Scenario: A full scenario runs as expected
 Given a thing is false
  And another thing is true
 When I do the old swaperoo
 Then the a thing should be true
  And anotherThing should be false
Tearing down revert anotherThing
Tearing down tearDown
`, Printer.(*testPrinter).Output(), "output should be as expected")
}

ensure's People

Contributors

iamkoch avatar

Stargazers

5hun Yanaura avatar Nick Bryan avatar Rob Weaver avatar Lim Leong Kui avatar Bryan Crossland avatar leo avatar Hamov Oleg avatar Boykiv Bogdan avatar w0i avatar John avatar Ruifeng Yue avatar Jose avatar Anthony HAMON avatar Jhonattan avatar Denis Titusov avatar  avatar Meysam P. Ganji avatar Krishnamurthy G B avatar Huseyin Caliskan avatar Lukas Stauersbøl avatar Peter Nguyen avatar  avatar Adam Zell avatar Juan Alvarez avatar Jeremy W. Sherman avatar Alexander Huck avatar Shashank Pachava avatar Lennie Budgell avatar Cody Krieger avatar Klaus Hartl avatar  avatar wolfi3 avatar Tim Kersey avatar

Watchers

 avatar Krishnamurthy G B avatar  avatar

Forkers

super-rain

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.