Question

They are both used to fulfill preconditions for a test. I can't really tell when I should use @Before (cucumber.api.java.Before) or when I should use @Given (cucumber.api.java.en.Given). When should I prefer/use one over the other?

Était-ce utile?

La solution

Use @Before, if a certain state need to be established for the test scenario to run successfully and business owners need not be aware of this step. Before is more behind-the-scenes than Given and does not have much of a business sense and does not need to be visible to the business owners. For example, "setting up variables, cleaning up cookies..etc".

Given, on the other hand, is a step that you want to make visible in the test scenario. Say, "I login to this App" kind of steps which make sense to the business world.

Autres conseils

I mostly use @Before for printing out the scenario name in the logs. Since @Before runs before every scenario, I add the code only once to any feature file and then its prints out the Scenario name before every scenario. This way its easier to keep a track of which Scenario is being executed. Code -

@Before
public void before(Scenario scenario) {
    System.out.println("------------------------------------------");
    System.out.println("Starting -> " + scenario.getName()); 
    System.out.println("------------------------------------------");
}

@After can also be used the same way. One additional usage of @After is to print out the scenario status using scenario.getStatus() method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top