gobdd icon indicating copy to clipboard operation
gobdd copied to clipboard

proposal: Code generation for test steps

Open bkielbasa opened this issue 5 years ago • 2 comments

Describe the solution you'd like We discussed in #65 that we want some kind of way of writing less test code. My idea is to add markers to functions which define the step definition. The first words should be GoBDD step: and after that, there should be a regular expression. Example:

// +gobdd-step: I add (\d+) and (\d+)
func add(ctx context.Context, var1, var2 int) error {
...
}

After calling command gobdd configure generate the command will output a new function definition.

func ConfigureSteps(suite *gobdd.Suite) {
    suite.AddStep(add, `I add (\d+) and (\d+)`)
    // other steps will go here
}

It will be copy&paste ready peace of code. If you provide the -f output.go parameter, the command will parse the output.go code:

  • if there's no ConfigureSteps func, it will add it as the very last function in the file
  • if it exists - it will replace its body.

bkielbasa avatar Feb 20 '20 16:02 bkielbasa

I like this and I would certainly use this in our team. Would it be clearer if the comment is in a form of a marker and starts with the + sign to differentiate it from normal comments? For example:

// +gobdd-step: I add (\d+) and (\d+)

Would there be also a viable option not to have the ConfigureSteps at all? I've looked deeply at how the https://magefile.org/ works - it's using both build tags and markers and auto-generates a temporary go file (could this in our case contain the ConfigureSteps?), which it then runs, and after the run it removes the temporary file. There's a flag not to delete the generated file, for debugging purposes etc.

mirogta avatar Feb 21 '20 15:02 mirogta

Thanks for your comment. I updated the description with the marker. About skipping the ConfigureSteps function. As I mentioned earlier, I'm not a huge fan of a lot of magic. And what's more, to do it we'll probably have to have an external binary which will generate and remove the file. That's what godog does and this is one of main reasons why this lib was created.

But, if you find an idea of improving it - please create the proposal :)

bkielbasa avatar Feb 21 '20 16:02 bkielbasa