Yashodip Patil
Yashodip Patil

Reputation: 79

How to use BeforeAll and AfterAll cucumber anonotation?

I want to execute the below some action before feature file.

public void beforeFeature() {
    File path = new File("D:\\AccelareWorkFitComplete\\ExtentsReports\\");

    File[] filename =path.listFiles();

    for(File fi:filename)
    {
        if (fi.toString().contains("Automation")) 
        {
            fi.delete();
        }
    }
}

Upvotes: 3

Views: 10383

Answers (2)

CeePlusPlus
CeePlusPlus

Reputation: 841

I would use Background (or @Before to execute behind the scenes). The official docs are here.

Feature File:

    Background:
       Given Files are deleted

    Scenerio: Test 1
       Then There are no files.

In my step definition file I would then do the following:

private static boolean testsInitialized = false

@Given("^Given files are deleted;$")
public void deleteFiles() {

if (!testsInitialized) {
    initializetest();
    testInitialized = true;
}

If you absolutely need Junit @BeforeClass equivalent, as of Cucumber 2.4.0, the following class works.

You can place this in test/java/cucumber/runtime.

package cucumber.runtime; //cannot change.  can be under /test/java

import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.snippets.FunctionNameGenerator;
import gherkin.pickles.PickleStep;

import java.util.List;

public class NameThisClassWhatever implements Backend {
    private Glue glue;
    private List<String> gluePaths;

    @Override
    public void loadGlue(Glue glue, List<String> gluePaths) {
        this.glue = glue;
        this.gluePaths = gluePaths;

        //Any before steps here
    }

    @Override
    public void disposeWorld() {
        //any after steps here
    }

    @Override
    public void setUnreportedStepExecutor(UnreportedStepExecutor executor) { }

    @Override
    public void buildWorld() { }

    @Override
    public String getSnippet(PickleStep p, String s, FunctionNameGenerator fng) {
        return null;
    }

    public NameThisClassWhatever(ResourceLoader resourceLoader)  { }
}

Upvotes: 1

kfairns
kfairns

Reputation: 3057

The beforeAll and afterAll hooks are for use around the entire of the cucumber suite, rather than around each feature.

A few versions back, there were beforeFeature and afterFeature hooks, but have been removed in more recent versions.

In summary, you could either go back a few versions to take advantage of the before/after feature functionality, or you could create a workaround in a before hook.

Upvotes: 1

Related Questions