golang BDD testcase framework.

时间:2021-07-19 16:35:19

BDD

What is Behaviour Driven Development and why should I care?

Behaviour Driven Development (BDD) is a testing methodology that has evolved from Test Driven Development (TDD). BDD is a useful way to describe your test cases from the perspective of the user. It uses a Given-When-Then format that is cognitively easy to parse. The output can be debugged and understood by both technology & business stakeholders. If you have used Behat, Cucumber etc in the past, this would be familiar to you.

Given: The user name is ABCD and the password is PWD
When: the user inputs these credentials and Clicks on enter
Then: the user should be logged in

Go ships with a fantastic testing library. More often than not, that testing library suffices the requirements. But, at Exotel, we’ve adopted the BDD methodology simply because it gives more user-readable expressions and some of the constraints imposed by the framework force developers to avoid cryptic error messages. Also, the long-term goal was to let the QA team or the business owners themselves to add test cases as and when they see something failing.

There are a bunch of decent BDD frameworks written for Go: Ginkgo , Goblin & GoConvey . We use Ginkgo heavily within our applications. Now, let’s get to the interesting part and show you how to use Ginkgo in your next application.

Popular Frameworks

ginkgo

prepare

$ go get github.com/onsi/ginkgo/ginkgo
$ go get github.com/onsi/gomega

$ cd book
$ ginkgo init # https://*.com/questions/25879473/how-do-i-set-up-a-ginkgo-test-suitee
$ ginkgo bootstrap $ cat book.go
 package book

 import (
_ "fmt"
) type Book struct {
Title string
Author string
Pages uint
} func (b *Book) CategoryByLength() string {
if b.Pages >= {
return "NOVEL"
}
return "SHORT STORY"
}

$ cat src_suite_test.go

 package book_test

 import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" "testing"
) func TestSrc(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Src Suite")
}

$ cat book_test.go

 package book_test

 import (
. "book" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
) var _ = Describe("Book", func() {
var (
longBook Book
shortBook Book
) BeforeEach(func() {
longBook = Book{
Title: "Les Miserables",
Author: "Victor Hugo",
Pages: ,
} shortBook = Book{
Title: "Fox In Socks",
Author: "Dr. Seuss",
Pages: ,
}
}) Describe("Categorizing book length", func() {
PContext("With more than 300 pages", func() {
It("should be a novel", func() {
Expect(longBook.CategoryByLength()).To(Equal("NOVEL"))
})
}) Context("With fewer than 300 pages", func() {
It("should be a short story", func() {
Expect(shortBook.CategoryByLength()).To(Equal("SHORT STORY"))
})
}) Context("Do panics test", func() { It("panics in a goroutine", func(done Done) {
go func() {
defer GinkgoRecover() Ω(func() bool { return true }()).Should(BeTrue()) close(done)
}()
})
})
})
})


Run Test

I try  $ ginkgo to run the test. no any response. so I use:

$ go test -ginkgo.v

Pending/Skip some test case

You do this by adding a P or an X in front of your Describe, Context, It, and Measure:

only test some case:

by adding an F in front of your Describe, Context, and It.

convey

Good to explain BDD with GO

https://semaphoreci.com/community/tutorials/getting-started-with-bdd-in-go-using-ginkgo

https://blog.codeship.com/implementing-a-bdd-workflow-in-go/