Created
March 9, 2019 17:35
-
-
Save zer0tonin/c5637ed53691ec3245518035b9799afc to your computer and use it in GitHub Desktop.
BDD in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: bank account | |
A user's bank account must be able to withdraw and deposit cash | |
Scenario Outline: Deposit | |
Given I have a bank account with <start>$ | |
When I deposit <deposit>$ | |
Then it should have a balance of <end>$ | |
Examples: | |
| start | deposit | end | | |
| 10 | 0 | 10 | | |
| 10 | 10 | 20 | | |
| 100 | 50 | 150 | | |
Scenario Outline: Withdrawal | |
Given I have a bank account with <start>$ | |
When I withdraw <withdrawal>$ | |
Then it should have a balance of <end>$ | |
Examples: | |
| start | withdrawal | end | | |
| 10 | 0 | 10 | | |
| 20 | 10 | 10 | | |
| 100 | 50 | 50 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bank | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"testing" | |
"github.com/DATA-DOG/godog" | |
) | |
var opt = godog.Options{ | |
Format: "progress", | |
} | |
func init() { | |
godog.BindFlags("godog.", flag.CommandLine, &opt) | |
} | |
func TestMain(m *testing.M) { | |
flag.Parse() | |
opt.Paths = flag.Args() | |
status := godog.RunWithOptions("godogs", func(s *godog.Suite) { | |
FeatureContext(s) | |
}, opt) | |
if st := m.Run(); st > status { | |
status = st | |
} | |
os.Exit(status) | |
} | |
var testAccount *account | |
func iHaveABankAccountWith(balance int) error { | |
testAccount = &account{balance:balance} | |
return nil | |
} | |
func iDeposit(amount int) error { | |
testAccount.deposit(amount) | |
return nil | |
} | |
func iWithdraw(amount int) error { | |
testAccount.withdraw(amount) | |
return nil | |
} | |
func itShouldHaveABalanceOf(balance int) error { | |
if testAccount.balance == balance { | |
return nil | |
} | |
return fmt.Errorf("Incorrect account balance") | |
} | |
func FeatureContext(s *godog.Suite) { | |
s.Step(`^I have a bank account with (\d+)\$$`, iHaveABankAccountWith) | |
s.Step(`^I deposit (\d+)\$$`, iDeposit) | |
s.Step(`^I withdraw (\d+)\$$`, iWithdraw) | |
s.Step(`^it should have a balance of (\d+)\$$`, itShouldHaveABalanceOf) | |
s.BeforeScenario(func(interface{}) { | |
testAccount = nil | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bank | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"testing" | |
"github.com/DATA-DOG/godog" | |
) | |
var opt = godog.Options{ | |
Format: "progress", | |
} | |
func init() { | |
godog.BindFlags("godog.", flag.CommandLine, &opt) | |
} | |
func TestMain(m *testing.M) { | |
flag.Parse() | |
opt.Paths = flag.Args() | |
status := godog.RunWithOptions("godogs", func(s *godog.Suite) { | |
FeatureContext(s) | |
}, opt) | |
if st := m.Run(); st > status { | |
status = st | |
} | |
os.Exit(status) | |
} | |
var testAccount *account | |
func iHaveABankAccountWith(balance int) error { | |
testAccount = &account{balance:balance} | |
return nil | |
} | |
func iDeposit(amount int) error { | |
testAccount.deposit(amount) | |
return nil | |
} | |
func iWithdraw(amount int) error { | |
testAccount.withdraw(amount) | |
return nil | |
} | |
func itShouldHaveABalanceOf(balance int) error { | |
if testAccount.balance == balance { | |
return nil | |
} | |
return fmt.Errorf("Incorrect account balance") | |
} | |
func FeatureContext(s *godog.Suite) { | |
s.Step(`^I have a bank account with (\d+)\$$`, iHaveABankAccountWith) | |
s.Step(`^I deposit (\d+)\$$`, iDeposit) | |
s.Step(`^I withdraw (\d+)\$$`, iWithdraw) | |
s.Step(`^it should have a balance of (\d+)\$$`, itShouldHaveABalanceOf) | |
s.BeforeScenario(func(interface{}) { | |
testAccount = nil | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module bank | |
require github.com/DATA-DOG/godog v0.7.10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/DATA-DOG/godog v0.7.10 h1:BRaQdCOsFth//Ep/J6gtb3xOeDh+sAVjL44ZdK1E9aI= | |
github.com/DATA-DOG/godog v0.7.10/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My version with negative values & the latest GoDog version: https://gist.github.com/davidaparicio/9fa9e664b6eed47e054ee6de426abe1a :)