ttutisani/Xunit.Gherkin.Quick

Missing support for Star Notation

richard-haslam opened this issue · 10 comments

Gherkin step keywords include * as well as Given, When, Then, And, But. It is normally used as a list of steps rather then precondition, action, result. Is it possible to implement this functionality?

References:
https://relishapp.com/cucumber/cucumber/docs/gherkin/using-star-notation-instead-of-given-when-then
https://stackoverflow.com/questions/27361647/cucumber-step-definitions-must-use-given-and-then-etc

I need to check if the Gherkin parser (used by Xunit.Gherkin.Quick) is able to successfully consume a scenario with * keyword. If that is true, then matching that with any other keyword must not be a problem.

Worth trying. Thanks for pointing out!

Also, did you notice the support of multiple step definition matches per method? Does that solve your use case?

Basically, you can have the same method execute for two or more steps if it has multiple keyword attributes attached to it. For example, every method in this class matches two steps each:

[FeatureFile("./Addition/AddTwoNumbers.feature")]
public sealed partial class AddTwoNumbers : Feature
{
    private readonly Calculator _calculator = new Calculator();

    [Given(@"I chose (\d+) as first number")]
    [When(@"I choose (\d+) as first number")]
    public void I_chose_first_number(int firstNumber)
    {
        _calculator.SetFirstNumber(firstNumber);
    }

    [And(@"I chose (\d+) as second number")]
    [And(@"I choose (\d+) as second number")]
    public void I_chose_second_number(int secondNumber)
    {
        _calculator.SetSecondNumber(secondNumber);
    }

    [And(@"I pressed add")]
    [And(@"I press add")]
    public void I_press_add()
    {
        _calculator.AddNumbers();
    }

    [Then(@"the result should be (\d+) on the screen")]
    [And(@"I saw (\d+) on the screen")]
    public void The_result_should_be_z_on_the_screen(int expectedResult)
    {
        var actualResult = _calculator.Result;

        Assert.Equal(expectedResult, actualResult);
    }
}

I have feature files for long end to end tests written in star notation that I need to implement, I can change them all to 'Given' but it makes them harder to read.

I took a quick look at the Gherkin parser and it looks like the languages.json file soes support *
https://github.com/cucumber/cucumber/blob/master/gherkin/dotnet/Gherkin/gherkin-languages.json

I've had a quick go at adding a class for * to a download of your code but I can't get it working. I must be missing something.

Thanks

I see. End to end tests can be really long.

While I do agree that Xunit.Gherkin.Quick should support the full gherkin syntax (i.e., star notation too), I still believe that using * must be the last resort. Given/When/Then/etc keywords add clarity as to what you are referring to. Given and optional Ands denote context (precondition), When and optional Ands denote action (important interactions with the system or scenario), Then and optional Ands denote post-conditions and assertions (to verify the scenario is going well).

When you are dealing with long end-to-end tests, I can envision using Given-When-Then several times in the same scenario. i.e., Given comes again after Then because the end-to-end test can consist of multiple sub-scenarios and that is fine and normally supported by Gherkin language.

So, in your concrete case, to improve the long-run maintainability of your test feature files, I would suggest that you should try to use one of the concrete keywords instead of using *. That is from a conceptual standpoint.

As I said, Xunit.Ghekin.Quick was designed to support the full Gherkin language syntax, so it makes sense that it can parse * too, but that's a technical implementation and has nothing to do with the best practices of using Gherkin and BDD as such.

Thanks, I completely agree with everything you have said above. I'm in a position of attempting to get colleagues from Specflow to dotnet core, in the theory we will be more platform agnostic (and solve some other problems in the process), so not totally in control of the feature files.

With a little more playing around I have managed to get the start notation working, and out it here: https://github.com/Richie5555/Xunit.Gherkin.Quick

There is a simple test extended to show it working but probably needs more, and I have added a doc for it.

Please do use this code if it saves you time adding this feature, else I will not be offended if you don't.

Thanks @Richie5555 , I will incorporate pieces of your work into the code change when I get to it. One thing I want to point out though - from what I can tell, the * means that it matches any of the existing keywords in the code. So when I implement it, probably there will be no [Star()] attribute. Instead, you use * in the feature file and it will match [Given] or any other existing attribute. At least that how I interpreted the links that you provided initially in this issue. Although I need to research more before implementing. I want to ensure that the implementation follows how it's supposed to be working. This is just a guess based on first sight. It will complicate the implementation, but that's a trade-off.

Please know that I am a little stuck with other high priority work right now (writing a book and trying to finish 3-rd part of it). I hope that I will add the support for the star notation within a month period. It may be a little too long, but I can't do anything about it (one thing at a time).
If you can't wait so long, consider generating a local nuget file out of your changes and use that for now. There is a pack.cmd file under /source/Xunit.Gherkin.Quick which creates the nuget file. Sorry if this causes any inconvenience!

When I add the star notation support, I will post back to this discussion. Thanks for your patience!

I think Specflow uses the Given keyword as well so would make sense to work in a familiar way for people.

I have done exactly what you have suggested with a NuGet feed and working well. No hurry to implement, and thanks for providing this resource in the first place!

Thanks.

This work is in progress on a star-notation-support branch. The work in progress can be previewed at https://github.com/ttutisani/Xunit.Gherkin.Quick/compare/star-notation-support?expand=1

New NuGet 3.5.0 is uploaded with the support of the star notation. It will be available for download after both the validation and indexing are complete (up to an hour). I am closing this ticket.