zoukankan      html  css  js  c++  java
  • golang BDD testcase framework.

    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://stackoverflow.com/questions/25879473/how-do-i-set-up-a-ginkgo-test-suitee
    $ ginkgo bootstrap

    $ cat book.go
     1 package book
     2 
     3 import (
     4         _ "fmt"
     5 )
     6 
     7 type Book struct {
     8         Title  string
     9         Author string
    10         Pages  uint
    11 }
    12 
    13 func (b *Book) CategoryByLength() string {
    14         if b.Pages >= 300 {
    15                 return "NOVEL"
    16         }
    17         return "SHORT STORY"
    18 }
    View Code

     $ cat src_suite_test.go

     1 package book_test
     2 
     3 import (
     4         . "github.com/onsi/ginkgo"
     5         . "github.com/onsi/gomega"
     6 
     7         "testing"
     8 )
     9 
    10 func TestSrc(t *testing.T) {
    11         RegisterFailHandler(Fail)
    12         RunSpecs(t, "Src Suite")
    13 }
    View Code

    $ cat book_test.go

     1 package book_test
     2 
     3 import (
     4         . "book"
     5 
     6         . "github.com/onsi/ginkgo"
     7         . "github.com/onsi/gomega"
     8 )
     9 
    10 var _ = Describe("Book", func() {
    11         var (
    12                 longBook  Book
    13                 shortBook Book
    14         )
    15 
    16         BeforeEach(func() {
    17                 longBook = Book{
    18                         Title:  "Les Miserables",
    19                         Author: "Victor Hugo",
    20                         Pages:  1488,
    21                 }
    22 
    23                 shortBook = Book{
    24                         Title:  "Fox In Socks",
    25                         Author: "Dr. Seuss",
    26                         Pages:  24,
    27                 }
    28         })
    29 
    30         Describe("Categorizing book length", func() {
    31                 PContext("With more than 300 pages", func() {
    32                         It("should be a novel", func() {
    33                                 Expect(longBook.CategoryByLength()).To(Equal("NOVEL"))
    34                         })
    35                 })
    36 
    37                 Context("With fewer than 300 pages", func() {
    38                         It("should be a short story", func() {
    39                                 Expect(shortBook.CategoryByLength()).To(Equal("SHORT STORY"))
    40                         })
    41                 })
    42 
    43                 Context("Do panics test", func() {
    44 
    45                         It("panics in a goroutine", func(done Done) {
    46                                 go func() {
    47                                         defer GinkgoRecover()
    48 
    49                                         Ω(func() bool { return true }()).Should(BeTrue())
    50 
    51                                         close(done)
    52                                 }()
    53                         })
    54                 })
    55         })
    56 })
    View Code


     

    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/

  • 相关阅读:
    超酷震撼 HTML5/CSS3动画应用及源码
    精美jQuery插件及源码 前端开发福利
    web前端炫酷实用的HTML5应用和jQuery插件
    8个web前端的精美HTML5 & CSS3效果及源码下载
    分享web前端七款HTML5 Loading动画特效集锦
    7款超酷HTML5 3D动画精选应用及源码
    8个WEB前端创意HTML5动画应用精选
    让人心动的jQuery插件和HTML5动画
    华丽的HTML5/jQuery动画和应用 前端必备
    炫酷实用的jQuery插件 涵盖菜单、按钮、图片
  • 原文地址:https://www.cnblogs.com/shaohef/p/7413762.html
Copyright © 2011-2022 走看看