goblin

package module
Version: v0.0.0-...-0a4f594 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2021 License: MIT Imports: 11 Imported by: 4

README

Goblin

Build Status Go Reportcard GoDoc License Release

A Mocha like BDD testing framework written in Go that requires no additional dependencies. Requires no extensive documentation nor complicated steps to get it running.

Why Goblin?

Inspired by the flexibility and simplicity of Node BDD and frustrated by the rigorousness of Go way of testing, we wanted to bring a new tool to write self-describing and comprehensive code.

What do I get with it?

  • Run tests as usual with go test
  • Colorful reports and beautiful syntax
  • Preserve the exact same syntax and behaviour as Node's Mocha
  • Nest as many Describe and It blocks as you want
  • Use Before, BeforeEach, After and AfterEach for setup and teardown your tests
  • No need to remember confusing parameters in Describe and It blocks
  • Use a declarative and expressive language to write your tests
  • Plug different assertion libraries
  • Gomega (supported so far)
  • Skip your tests the same way as you would do in Mocha
  • Automatic terminal support for colored outputs
  • Two line setup is all you need to get up running

How do I use it?

Since go test is not currently extensive, you will have to hook Goblin to it. You do that by adding a single test method in your test file. All your goblin tests will be implemented inside this function.

package foobar

import (
    "testing"
    . "github.com/franela/goblin"
)

func Test(t *testing.T) {
    g := Goblin(t)
    g.Describe("Numbers", func() {
        // Passing Test
        g.It("Should add two numbers ", func() {
            g.Assert(1+1).Equal(2)
        })
        // Failing Test
        g.It("Should match equal numbers", func() {
            g.Assert(2).Equal(4)
        })
        // Pending Test
        g.It("Should substract two numbers")
        // Excluded Test
        g.Xit("Should add two numbers ", func() {
            g.Assert(3+1).Equal(4)
        })
    })
}

Ouput will be something like:

Nice and easy, right?

Can I do asynchronous tests?

Yes! Goblin will help you to test asynchronous things, like goroutines, etc. You just need to add a done parameter to the handler function of your It. This handler function should be called when your test passes.

  ...
  g.Describe("Numbers", func() {
      g.It("Should add two numbers asynchronously", func(done Done) {
          go func() {
              g.Assert(1+1).Equal(2)
              done()
          }()
      })
  })
  ...

Goblin will wait for the done call, a Fail call or any false assertion.

How do I use it with Gomega?

Gomega is a nice assertion framework. But it doesn't provide a nice way to hook it to testing frameworks. It should just panic instead of requiring a fail function. There is an issue about that here. While this is being discussed and hopefully fixed, the way to use Gomega with Goblin is:

package foobar

import (
    "testing"
    goblin "github.com/franela/goblin"
    . "github.com/onsi/gomega"
)

func Test(t *testing.T) {
    g := goblin.Goblin(t)

    //special hook for gomega
    RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })

    g.Describe("lala", func() {
        g.It("lslslslsls", func() {
            Expect(1).To(Equal(10))
        })
    })
}

FAQ

How do I run specific tests?

If -goblin.run=$REGES is supplied to the go test command then only tests that match the supplied regex will run

Contributing

We do have a couple of issues pending. Feel free to contribute and send us PRs (with tests please 😄).

Special Thanks

Special thanks to Leandro Reox (Leitan) for the goblin logo.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveStack

func ResolveStack(skip int) []string

Types

type Assertion

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

Assertion represents a fact stated about a source object. It contains the source object and function to call

func (*Assertion) Eql

func (a *Assertion) Eql(dst interface{}, messages ...interface{})

Eql is a shorthand alias of Equal for convenience

func (*Assertion) Equal

func (a *Assertion) Equal(dst interface{}, messages ...interface{})

Equal takes a destination object and asserts that a source object and destination object are equal to one another. It will fail the assertion and print a corresponding message if the objects are not equivalent.

func (*Assertion) IsFalse

func (a *Assertion) IsFalse(messages ...interface{})

IsFalse asserts that a source is equal to false. Optional messages can be provided for inclusion in the displayed message if the assertion fails. It will fail the assertion if the source does not resolve to false.

func (*Assertion) IsNil

func (a *Assertion) IsNil(messages ...interface{})

IsNil asserts that source is nil.

func (*Assertion) IsNotNil

func (a *Assertion) IsNotNil(messages ...interface{})

IsNotNil asserts that source is not nil.

func (*Assertion) IsNotZero

func (a *Assertion) IsNotZero(messages ...interface{})

IsNotZero asserts the contrary of IsZero.

func (*Assertion) IsTrue

func (a *Assertion) IsTrue(messages ...interface{})

IsTrue asserts that a source is equal to true. Optional messages can be provided for inclusion in the displayed message if the assertion fails. It will fail the assertion if the source does not resolve to true.

func (*Assertion) IsZero

func (a *Assertion) IsZero(messages ...interface{})

IsZero asserts that source is a zero value for its respective type. If it is a structure, for example, all of its fields must have their respective zero value: "" for strings, 0 for int, etc. Slices, arrays and maps are only considered zero if they are nil. To check if these type of values are empty or not, use the len() from the data source with IsZero(). Example: g.Assert(len(list)).IsZero().

type Describe

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

type DetailedReporter

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

func (*DetailedReporter) Begin

func (r *DetailedReporter) Begin()

func (*DetailedReporter) BeginDescribe

func (r *DetailedReporter) BeginDescribe(name string)

func (*DetailedReporter) End

func (r *DetailedReporter) End()

func (*DetailedReporter) EndDescribe

func (r *DetailedReporter) EndDescribe()

func (*DetailedReporter) Failure

func (r *DetailedReporter) Failure(failure *Failure)

func (*DetailedReporter) ItFailed

func (r *DetailedReporter) ItFailed(name string)

func (*DetailedReporter) ItIsExcluded

func (r *DetailedReporter) ItIsExcluded(name string)

func (*DetailedReporter) ItIsPending

func (r *DetailedReporter) ItIsPending(name string)

func (*DetailedReporter) ItPassed

func (r *DetailedReporter) ItPassed(name string)

func (*DetailedReporter) ItTook

func (r *DetailedReporter) ItTook(duration time.Duration)

func (*DetailedReporter) SetTextFancier

func (r *DetailedReporter) SetTextFancier(f TextFancier)

type Done

type Done func(error ...interface{})

type Failure

type Failure struct {
	Stack    []string
	TestName string
	Message  string
}

type G

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

func Goblin

func Goblin(t *testing.T, arguments ...string) *G

func (*G) After

func (g *G) After(h func())

func (*G) AfterEach

func (g *G) AfterEach(h func())

func (*G) Assert

func (g *G) Assert(src interface{}) *Assertion

func (*G) Before

func (g *G) Before(h func())

func (*G) BeforeEach

func (g *G) BeforeEach(h func())

func (*G) Describe

func (g *G) Describe(name string, h func())

func (*G) Errorf

func (g *G) Errorf(format string, args ...interface{})

func (*G) Fail

func (g *G) Fail(error interface{})

func (*G) FailNow

func (g *G) FailNow()

func (*G) Failf

func (g *G) Failf(format string, args ...interface{})

func (*G) Fatalf

func (g *G) Fatalf(format string, args ...interface{})

func (*G) Helper

func (g *G) Helper()

func (*G) It

func (g *G) It(name string, h ...interface{})

func (*G) JustBeforeEach

func (g *G) JustBeforeEach(h func())

func (*G) SetReporter

func (g *G) SetReporter(r Reporter)

func (*G) Timeout

func (g *G) Timeout(time time.Duration)

func (*G) Xit

func (g *G) Xit(name string, h ...interface{})

type It

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

type Itable

type Itable interface {
	// contains filtered or unexported methods
}

type Monochrome

type Monochrome struct {
}

func (*Monochrome) Cyan

func (self *Monochrome) Cyan(text string) string

func (*Monochrome) Gray

func (self *Monochrome) Gray(text string) string

func (*Monochrome) Green

func (self *Monochrome) Green(text string) string

func (*Monochrome) Red

func (self *Monochrome) Red(text string) string

func (*Monochrome) WithCheck

func (self *Monochrome) WithCheck(text string) string

func (*Monochrome) Yellow

func (self *Monochrome) Yellow(text string) string

type Reporter

type Reporter interface {
	BeginDescribe(string)
	EndDescribe()
	Begin()
	End()
	Failure(*Failure)
	ItTook(time.Duration)
	ItFailed(string)
	ItPassed(string)
	ItIsPending(string)
	ItIsExcluded(string)
}

type Runnable

type Runnable interface {
	// contains filtered or unexported methods
}

type TerminalFancier

type TerminalFancier struct {
}

func (*TerminalFancier) Cyan

func (self *TerminalFancier) Cyan(text string) string

func (*TerminalFancier) Gray

func (self *TerminalFancier) Gray(text string) string

func (*TerminalFancier) Green

func (self *TerminalFancier) Green(text string) string

func (*TerminalFancier) Red

func (self *TerminalFancier) Red(text string) string

func (*TerminalFancier) WithCheck

func (self *TerminalFancier) WithCheck(text string) string

func (*TerminalFancier) Yellow

func (self *TerminalFancier) Yellow(text string) string

type TextFancier

type TextFancier interface {
	Red(text string) string
	Gray(text string) string
	Cyan(text string) string
	Green(text string) string
	Yellow(text string) string
	WithCheck(text string) string
}

type Xit

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

Jump to

Keyboard shortcuts

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