franela / goblin

Minimal and Beautiful Go testing framework
MIT License
884 stars 79 forks source link

Add JustBeforeEach clause similar to ginkgo which adds more granularity #72

Closed snobb closed 5 years ago

snobb commented 5 years ago

JustBeforeEach allows you to decouple creation from intialization. Initalization occurs in the JustBeforeEach using configuration specified and modified by a chain of BeforeEachs. This allows for a better code reuse in tests.

JustBeforeEach fires after BeforeEach but before It for each It in the block. Similar to BeforeEach blocks JustBeforeEach from outer blocks has presedence over the local JustBeforeEach.

For more info: https://onsi.github.io/ginkgo/#separating-creation-and-configuration-justbeforeeach

The JustBeforeEach clause allows for doing this:

var (
    param1, param2 int
    inst           Tested
)

g.Describe("Test Suite", func() {
    g.JustBeforeEach(func() {
        inst = NewTested(param1, param2)
    })

    g.Describe("Success", func() {
        g.BeforeEach(func() {
            param1 = 1
            param2 = 2
        })

        g.It("should succeed", func() {
            fmt.Println("inst initialized with new param1 and param2")
        })
    })

    g.Describe("Failure", func() {
        g.BeforeEach(func() {
            param1 = -1
            param2 = -2
        })

        g.It("should fail", func() {
            fmt.Println("inst initialized with new negative param1 and param2")
        })
    })
})
marcosnils commented 5 years ago

According to the ginkgo documentation justBeforeEach solved some BDD anti pattern which is related to code reuse. I've been looking at other popular BDD libraries like mocha and jasmine and none have implemented and discussed this pattern. Even though I agree that it helps to reduce code duplication, I believe it also adds some complexity in understanding the way things run during tests. I'm not entirely convinced to merge this due to this last comment.

Is there any other strong argument to include this @snobb which I might be missing?. Don't get me wrong, I do believe it's useful it's just that I just don't want to add stuff just because some other framework has it.

snobb commented 5 years ago

We've been using ginkgo and currently want to move to goblin mostly because it's smaller/simpler and has no dependencies. Our tests heavily relied on the JustBeforeEach though and thus converting our tests means we have to introduce helper lambda funcs everywhere it's been used (or just hard code the same code over and over). Given the ease of adding this functionality I thought it would be nice to just have it. There is no obligation to use it nor it brakes any existing tests - just more granularity at a cost of a little change. I am also a heavy user of mocha and think mocha could also benefit from this (IMHO). It's much easier to get around the lack of this feature in nodejs though. :)

marcosnils commented 5 years ago

Fair enough. Let's merge this

snobb commented 5 years ago

Thanks a lot!