go-bdd / gobdd

BDD framework
https://go-bdd.github.io/gobdd/
MIT License
115 stars 19 forks source link

proposal: Code generation for test steps #72

Open bkielbasa opened 4 years ago

bkielbasa commented 4 years ago

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:

mirogta commented 4 years ago

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.

bkielbasa commented 4 years ago

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 :)