gherkingen
It's a Behaviour Driven Development (BDD) tests generator for Golang.
It accepts a *.feature
Cucumber/Gherkin file and generates a test boilerplate. All that remains is to change the tests a little. The generator supports go generate
and go test
for generated tests.
The generator is very customizable, it is possible to customize an output for any golang testing framework or even for another language.
Simple example
What is for?
Feature: Application command line tool
Scenario: User wants to see usage information
When <flag> is provided
Then usage should be printed
Examples:
| <flag> |
| --help |
| -help |
Then this generator writes a golang output:
func TestApplicationCommandLineTool(t *testing.T) {
f := bdd.NewFeature(t, "Application command line tool")
f.Scenario("User wants to see usage information", func(t *testing.T, f *bdd.Feature) {
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--help": {"--help"},
"-help": {"-help"},
}
for name, tc := range testCases {
name, tc := name, tc
f.TestCase(name, tc, func(t *testing.T, f *bdd.Feature) {
f.When("<flag> is provided", func() {
})
f.Then("usage should be printed", func() {
})
})
}
})
}
Then on failure next logs will be printed:
Feature: Application command line tool
Scenario: User wants to see usage information
# TestCase: {Flag:-help}
When: -help is provided
Then: usage should be printed
More advanced example
See internal/app/app.feature and internal/app/app_test.go.
Install
Run:
go install github.com/hedhyw/gherkingen/cmd/gherkingen@latest
Simple usage
Usage
For generating test output, simply run:
gherkingen EXAMPLE.feature
More advanced usage
Generating test output with custom options
gherkingen \
-format go \
-template my_template.tmpl \
EXAMPLE.feature
Listing internal templates
gherkingen -list
Help
gherkingen --help
Usage of gherkingen [FEATURE_FILE]:
-format string
output format: json, go, raw (default "go")
-help
print usage
-list
list internal templates
-template string
template file (default "@/std.struct.v1.go.tmpl")
Custom templates
Output customization
You can provide your own template, it can be based on internal/assets/std.args.v1.go.tmpl. In the command-line tool specify the template using -template
flag: gherkingen -template example.tmpl raw example.feature
Frameworks support
It is possible to integrate the generator with any BDD-testing fraemwork. Feel free to create a pull request for supporting templates for them. For this:
- Create a template
internal/assets/SOME_NAME.go.tmpl
. - Add it to the test
TestOpenTemplate
in the file internal/assets/assets_test.go. - Check:
make lint test
. - Commit&Push, create a PR.
Language support
Templates are very customizable, so you can even generate non-golang code. In the command-line tool specify raw
format using -format
flag and your template using -template
flag: gherkingen -format raw -template example.tmpl example.feature
.
License
See License.