github.com/maps90/godog@v0.7.5-0.20170923143419-0093943021d4/README.md (about) 1 [![Build Status](https://travis-ci.org/DATA-DOG/godog.svg?branch=master)](https://travis-ci.org/DATA-DOG/godog) 2 [![GoDoc](https://godoc.org/github.com/DATA-DOG/godog?status.svg)](https://godoc.org/github.com/DATA-DOG/godog) 3 [![codecov.io](https://codecov.io/github/DATA-DOG/godog/branch/master/graph/badge.svg)](https://codecov.io/github/DATA-DOG/godog) 4 5 # Godog 6 7 <p align="center"><img src="/logo.png" alt="Godog logo" style="width:250px;" /></p> 8 9 **The API is likely to change a few times before we reach 1.0.0** 10 11 Please read all the README, you may find it very useful. And do not forget 12 to peek into the 13 [CHANGELOG](https://github.com/DATA-DOG/godog/blob/master/CHANGELOG.md) 14 from time to time. 15 16 Package godog is the official Cucumber BDD framework for Golang, it merges 17 specification and test documentation into one cohesive whole. The author 18 is a core member of [cucumber team](https://github.com/cucumber). 19 20 The project is inspired by [behat][behat] and [cucumber][cucumber] and is 21 based on cucumber [gherkin3 parser][gherkin]. 22 23 **Godog** does not intervene with the standard **go test** command 24 behavior. You can leverage both frameworks to functionally test your 25 application while maintaining all test related source code in **_test.go** 26 files. 27 28 **Godog** acts similar compared to **go test** command, by using go 29 compiler and linker tool in order to produce test executable. Godog 30 contexts need to be exported the same way as **Test** functions for go 31 tests. Note, that if you use **godog** command tool, it will use `go` 32 executable to determine compiler and linker. 33 34 **Godog** ships gherkin parser dependency as a subpackage. This will 35 ensure that it is always compatible with the installed version of godog. 36 So in general there are no vendor dependencies needed for installation. 37 38 The following about section was taken from 39 [cucumber](https://cucumber.io/) homepage. 40 41 ## About 42 43 #### A single source of truth 44 45 Cucumber merges specification and test documentation into one cohesive whole. 46 47 #### Living documentation 48 49 Because they're automatically tested by Cucumber, your specifications are 50 always bang up-to-date. 51 52 #### Focus on the customer 53 54 Business and IT don't always understand each other. Cucumber's executable 55 specifications encourage closer collaboration, helping teams keep the 56 business goal in mind at all times. 57 58 #### Less rework 59 60 When automated testing is this much fun, teams can easily protect 61 themselves from costly regressions. 62 63 ## Install 64 65 go get github.com/DATA-DOG/godog/cmd/godog 66 67 ## Example 68 69 The following example can be [found 70 here](/examples/godogs). 71 72 ### Step 1 73 74 Given we create a new go package **$GOPATH/src/godogs**. From now on, this 75 is our work directory `cd $GOPATH/src/godogs`. 76 77 Imagine we have a **godog cart** to serve godogs for lunch. First of all, 78 we describe our feature in plain text - `vim 79 $GOPATH/src/godogs/features/godogs.feature`: 80 81 ``` gherkin 82 # file: $GOPATH/src/godogs/features/godogs.feature 83 Feature: eat godogs 84 In order to be happy 85 As a hungry gopher 86 I need to be able to eat godogs 87 88 Scenario: Eat 5 out of 12 89 Given there are 12 godogs 90 When I eat 5 91 Then there should be 7 remaining 92 ``` 93 94 **NOTE:** same as **go test** godog respects package level isolation. All 95 your step definitions should be in your tested package root directory. In 96 this case - `$GOPATH/src/godogs` 97 98 ### Step 2 99 100 If godog is installed in your GOPATH. We can run `godog` inside the 101 **$GOPATH/src/godogs** directory. You should see that the steps are 102 undefined: 103 104 ![Undefined step snippets](/screenshots/undefined.png?raw=true) 105 106 If we wish to vendor godog dependency, we can do it as usual, using tools 107 you prefer: 108 109 git clone https://github.com/DATA-DOG/godog.git $GOPATH/src/godogs/vendor/github.com/DATA-DOG/godog 110 111 It gives you undefined step snippets to implement in your test context. 112 You may copy these snippets into your `godogs_test.go` file. 113 114 Our directory structure should now look like: 115 116 ![Directory layout](/screenshots/dir-tree.png?raw=true) 117 118 If you copy the snippets into our test file and run godog again. We should 119 see the step definition is now pending: 120 121 ![Pending step definition](/screenshots/pending.png?raw=true) 122 123 You may change **ErrPending** to **nil** and the scenario will 124 pass successfully. 125 126 Since we need a working implementation, we may start by implementing only what is necessary. 127 128 ### Step 3 129 130 We only need a number of **godogs** for now. Lets keep it simple. 131 132 ``` go 133 /* file: $GOPATH/src/godogs/godogs.go */ 134 package main 135 136 // Godogs available to eat 137 var Godogs int 138 139 func main() { /* usual main func */ } 140 ``` 141 142 ### Step 4 143 144 Now lets implement our step definitions, which we can copy from generated 145 console output snippets in order to test our feature requirements: 146 147 ``` go 148 /* file: $GOPATH/src/godogs/godogs_test.go */ 149 package main 150 151 import ( 152 "fmt" 153 154 "github.com/DATA-DOG/godog" 155 ) 156 157 func thereAreGodogs(available int) error { 158 Godogs = available 159 return nil 160 } 161 162 func iEat(num int) error { 163 if Godogs < num { 164 return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs) 165 } 166 Godogs -= num 167 return nil 168 } 169 170 func thereShouldBeRemaining(remaining int) error { 171 if Godogs != remaining { 172 return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs) 173 } 174 return nil 175 } 176 177 func FeatureContext(s *godog.Suite) { 178 s.Step(`^there are (\d+) godogs$`, thereAreGodogs) 179 s.Step(`^I eat (\d+)$`, iEat) 180 s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining) 181 182 s.BeforeScenario(func(interface{}) { 183 Godogs = 0 // clean the state before every scenario 184 }) 185 } 186 ``` 187 188 Now when you run the `godog` again, you should see: 189 190 ![Passed suite](/screenshots/passed.png?raw=true) 191 192 We have hooked to **BeforeScenario** event in order to reset application 193 state before each scenario. You may hook into more events, like 194 **AfterStep** to print all state in case of an error. Or 195 **BeforeSuite** to prepare a database. 196 197 By now, you should have figured out, how to use **godog**. Another advice 198 is to make steps orthogonal, small and simple to read for an user. Whether 199 the user is a dumb website user or an API developer, who may understand 200 a little more technical context - it should target that user. 201 202 When steps are orthogonal and small, you can combine them just like you do 203 with Unix tools. Look how to simplify or remove ones, which can be 204 composed. 205 206 ### References and Tutorials 207 208 - [how to use godog by semaphoreci](https://semaphoreci.com/community/tutorials/how-to-use-godog-for-behavior-driven-development-in-go) 209 - see [examples](https://github.com/DATA-DOG/godog/tree/master/examples) 210 - see extension [AssistDog](https://github.com/hellomd/assistdog), which 211 may have useful **gherkin.DataTable** transformations or comparison 212 methods for assertions. 213 214 ### Documentation 215 216 See [godoc][godoc] for general API details. 217 See **.travis.yml** for supported **go** versions. 218 See `godog -h` for general command options. 219 220 See implementation examples: 221 222 - [rest API server](/examples/api) 223 - [rest API with Database](/examples/db) 224 - [godogs](/examples/godogs) 225 226 ## FAQ 227 228 ### Running Godog with go test 229 230 You may integrate running **godog** in your **go test** command. You can 231 run it using go [TestMain](https://golang.org/pkg/testing/#hdr-Main) func 232 available since **go 1.4**. In this case it is not necessary to have 233 **godog** command installed. See the following example: 234 235 ``` go 236 func TestMain(m *testing.M) { 237 status := godog.RunWithOptions("godog", func(s *godog.Suite) { 238 FeatureContext(s) 239 }, godog.Options{ 240 Format: "progress", 241 Paths: []string{"features"}, 242 Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order 243 }) 244 245 if st := m.Run(); st > status { 246 status = st 247 } 248 os.Exit(status) 249 } 250 ``` 251 252 You can even go one step further and reuse **go test** flags, like 253 **verbose** mode in order to switch godog **format**. See the following 254 example: 255 256 ``` go 257 func TestMain(m *testing.M) { 258 format := "progress" 259 for _, arg := range os.Args[1:] { 260 if arg == "-test.v=true" { // go test transforms -v option 261 format = "pretty" 262 break 263 } 264 } 265 status := RunWithOptions("godog", func(s *Suite) { 266 SuiteContext(s) 267 }, Options{ 268 Format: format, 269 Paths: []string{"features"}, 270 }) 271 272 if st := m.Run(); st > status { 273 status = st 274 } 275 os.Exit(status) 276 } 277 ``` 278 279 Now when running `go test -v` it will use **pretty** format. 280 281 ### Configure common options for godog CLI 282 283 There are no global options or configuration files. Alias your common or 284 project based commands: `alias godog-wip="godog --format=progress 285 --tags=@wip"` 286 287 ### Testing browser interactions 288 289 **godog** does not come with builtin packages to connect to the browser. 290 You may want to look at [selenium](http://www.seleniumhq.org/) and 291 probably [phantomjs](http://phantomjs.org/). See also the following 292 components: 293 294 1. [browsersteps](https://github.com/llonchj/browsersteps) - provides 295 basic context steps to start selenium and navigate browser content. 296 2. You may wish to have [goquery](https://github.com/PuerkitoBio/goquery) 297 in order to work with HTML responses like with JQuery. 298 299 ### Concurrency 300 301 In order to support concurrency well, you should reset the state and 302 isolate each scenario. They should not share any state. It is suggested to 303 run the suite concurrently in order to make sure there is no state 304 corruption or race conditions in the application. 305 306 It is also useful to randomize the order of scenario execution, which you 307 can now do with **--random** command option. 308 309 **NOTE:** if suite runs with concurrency option, it concurrently runs 310 every feature, not scenario per different features. This gives 311 a flexibility to isolate state per feature. For example using 312 **BeforeFeature** hook, it is possible to spin up costly service and shut 313 it down only in **AfterFeature** hook and share the service between all 314 scenarios in that feature. It is not advisable though, because you are 315 risking having a state dependency. 316 317 ## Contributions 318 319 Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) - 320 please open an issue before to discuss whether these changes can be accepted. All backward incompatible changes are 321 and will be treated cautiously. 322 323 ## License 324 325 **Godog** is licensed under the [three clause BSD license][license] 326 327 **Gherkin** is licensed under the [MIT][gherkin-license] and developed as 328 a part of the [cucumber project][cucumber] 329 330 [godoc]: http://godoc.org/github.com/DATA-DOG/godog "Documentation on godoc" 331 [golang]: https://golang.org/ "GO programming language" 332 [behat]: http://docs.behat.org/ "Behavior driven development framework for PHP" 333 [cucumber]: https://cucumber.io/ "Behavior driven development framework" 334 [gherkin]: https://github.com/cucumber/gherkin-go "Gherkin3 parser for GO" 335 [gherkin-license]: https://en.wikipedia.org/wiki/MIT_License "The MIT license" 336 [license]: http://en.wikipedia.org/wiki/BSD_licenses "The three clause BSD license"