Git Product home page Git Product logo

zen's Introduction

Zen

BDD Testing Framework For Go.

go get github.com/pranavraja/zen

Forked from github.com/azer/mao.

Changes in my fork:

  • Print each behaviour as it's being verified
  • Actually fail the testrunner when there are expectation failures
  • Small API changes
  • Allow custom matchers
package zen

import (
    "testing"
    . "github.com/pranavraja/zen"
)

func TestZen(t *testing.T) {
    Desc(t, "zen", func(it It) {
        it("should know when things exist", func(expect Expect) {
            expect("tree").ToExist()
        })
        it("should know when things don't exist", func(expect Expect) {
            expect(nil).ToNotExist()
        })
        it("should know that a thing is equal to itself", func(expect Expect) {
            expect(1).ToEqual(1)
        })
        it("should know when things are different", func(expect Expect) {
            expect(1).ToNotEqual(2)
        })
        it("should be able to learn about new things", func (expect Expect) {
            divisibleBy := func (a, b interface{}) bool {
                return a.(int) % b.(int) == 0
            }
            expect(1).To("be divisible by", divisibleBy, 1)
        })
    })
}

Output

Pass:

Test passed

Fail:

Test failure

Before and After test setup

package zen

import (
    "testing"
    . "github.com/pranavraja/zen"
)

func TestZen(t *testing.T) {
    Desc(t, "before and after", func(it It) {
        count := 0

        before := func() {
          count++
        }

        after := func() {
          count--
        }

        setup := Setup(before, after)

        it("should execute before and after functions", setup(func(expect Expect) {
            expect(count).ToEqual(1)
        }))

        it("should execute before and after functions", setup(func(expect Expect) {
            expect(count).ToEqual(1)
        }))
    })
}

zen's People

Contributors

pranavraja avatar eduncan911 avatar nerdyworm avatar

Stargazers

Maksym Ivanov avatar Oleg Prozorov avatar Angel Santiago Jaime Zavala avatar Lily Lee avatar 田磊 [Lei Tian] avatar Michael S. Manley avatar Cause Chung avatar Maksim Sadakov avatar Dmitry Ledentsov avatar vincent avatar Gabriel Pedro avatar onceknown avatar Jacob Moncur avatar Andrew Gerst avatar flyer avatar Spiros Gerokostas avatar Scot Wells avatar Chris Hart avatar Peter P. Gengler avatar Hans Rødtang avatar  avatar Michael Reynolds avatar Yuri Teixeira avatar Herbert Fischer avatar Igor P. Leroy avatar Claudson Oliveira avatar Avelino avatar Brad Rydzewski avatar canavar avatar Scott Willeke avatar Sergey Todyshev avatar Petri Kola avatar  avatar  avatar Baldur Kristinsson avatar taichi avatar Andrei Dragomir avatar David T. Lehmann avatar tobynet avatar Takuya Ueda avatar Nikolay Burlov avatar marian kopriva avatar Márk Bartos avatar Allan Waddell avatar Shilo Ayalon avatar Deathwish avatar Jocelyn Boullier avatar Jim Fleming avatar JP avatar Lionel Barrow avatar  avatar Gus Becciu avatar Martin Zapata avatar Senthil Arivudainambi avatar

Watchers

 avatar James Cloos avatar  avatar

zen's Issues

Enabled BDD Scenarios

I submitted a tiny pull request lately to enable a test. The code below is completely different than that.

My main purpose of forking was to change the code to be more like Dan North's original BDD definitions in his original 2008 introduction of BDD. Aka, the "purist" vision of BDD with Given/When/Then that follows the overall scenario:

Given some initial context (the givens),
When an event occurs,
then ensure some outcomes.

I also come from a .NET background where I was an avid user of Machine.Specifications (aka MSpec) BDD framework and his patterns. More specifically, his very nicely formatted TEXT and HTML outputs that was completely human readable.

Therefore, I present the following updates to Go's Zen BDD Framework: Full Contextual Scenarios

// bdd_test.go
//
func TestBddSceneriosUsingContextAndSpecifications(t *testing.T) {

    Given(t, "a BDD scenario", func(when When) {

        when("an event occurs", func(it It) {
            it("should evaluate to 1", func(expect Expect) {
                expect(1).ToEqual(1)
            })

            it("should also evaluate to 3", func(expect Expect) {
                expect(3).ToEqual(3)
            })

            it("should perform another evaluation", func(expect Expect) {
                expect(4).ToNotEqual(5)
            })

            it("should also perform another evaluation", func(expect Expect) {
                expect("hellow").ToNotEqual("world")
            })
        })

        // common context
        count := 0

        before := func() {
            count++
        }

        after := func() {
            count--
        }

        setup := Setup(before, after)

        when("using Setup() in extended-style", func(it It) {
            it("should increment count to 1", setup(func(expect Expect) {
                expect(count).ToEqual(1)
            }))

            if count != 0 {
                t.Error("In BDD-specs, count should have been reset to zero by the teardown func")
            }
        })
    })
}

Which outputs the following:

 Given a BDD scenario 
   When an event occurs 
     It should evaluate to 1 
     It should also evaluate to 3 
     It should perform another evaluation 
     It should also perform another evaluation 
   When using Setup() in extended-style 
     It should increment count to 1 
PASS
ok      github.com/eduncan911/zen   0.008s

Note my new tests that start at the "Given a BDD scenario". You will also notice that I inserted "Given / When / It" in the output.

If you are interested in a Pull request for these changes, let me know. You can have a look at the changes here:

eduncan911@91df917

In short:

  • extended existing code and keeping it backwards compatible with all existing Zen projects,
  • the only breaking change is any custom formatter(s) will need to implement two new methods,
  • broke apart but also grouped Desc from Titles for all existing,
  • enabled grouping of output indentions

It remains completely compatible with all existing Zen users. Just the output is formatted differently. If you run all tests, you will the output has been tweaked to group multiple existing it() funcs:

eduncanmac1:~/go/src/github.com/eduncan911/zen [master +0 ~5 -0]$ go test
   When Equality Specs 
     It should have an integer equal to itself 
     It should not have any integer equal to nil 
   When Setup Specs 
     It should execute before by incrementing count 
 Given a BDD scenario 
   When an event occurs 
     It should evaluate to 1 
     It should also evaluate to 3 
     It should perform another evaluation 
     It should also perform another evaluation 
   When using Setup() in extended-style 
     It should increment count to 1 
PASS
ok      github.com/eduncan911/zen   0.008s

That runs all existing tests before my changes indicating it remains backwards compatible; then, my new scenario specs are run along side them seamlessly.

Well, there is a breaking change: it is for anyone using custom formatters - I extended the interface to have two additional methods:

type formatter interface {
    PrintContext()
    PrintWhen()
    PrintTitle()
    PrintError(string)
}

You'll need to implement a PrintContext() and PrintWhen(). If anyone really has overwritten the default formatter, I'm sure this will be a welcome upgrade/tweak.

It's also missing documentation which I am happy to add later in another pull request.

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.