Search code examples
c#automationbddacceptance-testinggherkin

Given/When/Then - Can I use a Given "statement" in more than one operation say Given and When both


Suppose I got the BDD feature file with Given step say,

Given User is logged in as administrator
When some functionality line
Then some functionality line

and i want to use "User is logged in as administrator" as a When statement in some other scenario how can I do that..

I tried writing,

[Given,When(@"User is logged in as administrator")] 
public void abc(){
//my operation
}

specflow allows me to write in such a way using (cntrl+spacebar) but if I try to navigate it only navigates to "When User is logged in as administrator"

It's only navigating to the last value which u write [Given,When("..")]. Here, it navigates to When..

How can I make it work for both statements since in some scenarios,

administrator is logged in statement is a pre-requiste but in some scenario,

I have to login and it's an action which I need to perform hence need to also write the same statement as "When" statement

Is there any way by which I can use the same statement as Given and When??

Thanks in advance


Solution

  • First, I want to check that you really need that "When" at all.

    If you're not actually writing custom logging-in functionality, consider removing the "When" scenario altogether. Logging in, once written, rarely changes, and is easy to separate from other functionality. Test it manually then leave it alone. It's OK to use the functionality to set up a logged-in account for other scenarios though.

    If you're using the logging in step as part of something larger, with multiple Whens, you probably want to find a way to phrase the thing you're trying to do as one step, and call the logging in step within that. Generally scenarios only have one When.

    If neither of those apply, then, okay, you need a scenario in which your user actively logs in. The behaviour of logging in is interesting, and you'd like to provide an example.

    From a linguistic perspective, I wouldn't want to see the same phrasing for a Given as I would for a When.

    The idea of a Given is that it doesn't matter how you got there. The behaviour is unimportant for a scenario. The step describes a context that exists. The user is logged in. How did they log in? Who knows? Who cares? It's not important.

    For the When, though, the action is active. Someone, or something is performing an event. The user logs in.

    So, I'd prefer to see this:

    [Given(@"User is logged in as an adminstrator")]
    [When(@"User logs in as administrator")] 
    public void abc(){
        //my operation
    }
    

    together with steps which match those. This still lets you reuse your functionality for now, but it also gives you the option to separate them later. That might be important if, for instance, logging in turns out to be an expensive step and you decide you want to reuse a logged-in account for all those scenarios in which it's merely a Given.

    More on English tenses and BDD here.