BDD with Cucumber
Behavior-Driven Development (BDD) helps teams write human-readable descriptions of software behavior in a structured format. Vyndara uses this development format to guarantee every logic can be understood easily and is covered by verifiable test cases.
In Go, we use Godog, a Cucumber implementation, to execute these specifications.
This guide introduces BDD with Godog and provides a structured test setup.
Getting Started
Ensure you have the Arematics testing library go-tests
installed:
go install github.com/Arematics/go-tests
Then, write your feature files inside a features/
directory using Gherkin syntax.
Test Initialization
We use a helper function to initialize our Godog tests:
func InitializeTests(t *testing.T, initializer func(ctx *godog.ScenarioContext), featurePath string, suiteName string) {
Test = t
options := &godog.Options{
Format: "pretty",
Output: colors.Colored(os.Stdout),
Paths: []string{featurePath},
}
suite := godog.TestSuite{
Name: suiteName,
ScenarioInitializer: initializer,
Options: options,
}
if status := suite.Run(); status != 0 {
t.Fail()
}
}
This function runs a Godog test suite based on the provided feature path and scenario initializer.
Defining Common Steps
Godog binds test scenarios to step definitions. For example you can write Scenario definitions like that:
func initCommonServiceTests(ctx *godog.ScenarioContext) {
Init(context.Background()) // Initialize environment in go-orm
ctx.Step(`^a database is available$`, aDatabaseIs)
ctx.Step(`^I create an entity with test value "([^"]*)"$`, canCreateValue)
ctx.Step(`^I should be able to retrieve an entity with value "([^"]*)"$`, canReadItAndHaveValue)
ctx.Step(`^I can update the entity to have the value "([^"]*)"$`, canUpdateItWithValue)
ctx.Step(`^I can delete the entity$`, canDeleteIt)
ctx.Step(`^it’s no longer available$`, isDeleted)
}
These steps map Gherkin statements to Go functions.
Running Feature Tests
We call InitializeTests
from the go-tests
library for each feature file:
func TestServiceFeatures(t *testing.T) {
tests.InitializeTests(
t,
initCommonServiceTests,
"./features/basic_service_operations.feature",
"Basic Service Operations",
)
}
This ensures multiple test suites can run independently.
Running the Tests
Execute the tests using:
go test -v ./...
Conclusion
With BDD using Godog, tests remain understandable for both developers and non-technical stakeholders. This structured approach ensures feature validation in a human-readable format while integrating seamlessly with Go.