gherkingen

module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2022 License: MIT

README

gherkingen

Version Build Status Go Report Card Coverage Status

It's a Behaviour Driven Development (BDD) tests generator for Golang.

Golang logo

It accepts a *.feature Cucumber/Gherkin file and generates a test boilerplate. All that remains is to change the tests a little.

The generator is very customizable, it is possible to customize an output for any golang testing framework or even for another language.

What is for?

Simple example

Given feature [reference]:

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

Usage

Simple 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")

Output customization

Custom templates

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:

  1. Create a template internal/assets/SOME_NAME.go.tmpl.
  2. Add it to the test TestOpenTemplate in the file internal/assets/assets_test.go.
  3. Check: make lint test.
  4. 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.

Directories

Path Synopsis
cmd
internal
app
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL