Search code examples
cucumber-jvmcucumber-junitjira-xray

Run Cucumber JVM @BeforeClass Without Feature Files


The title may be a bit confusing at this point; hopefully I can clear it up.

What I Have

I'm running Cucumber JVM with Selenium WebDriver to automate our system test cases. These test cases are currently stored in JIRA using the XRay Test Management plugin. XRay also provides APIs to fetch the feature files as well as upload the results back to JIRA.

I have created a custom JIRA utility class to download the tests as feature files and upload the test results from and to JIRA - as well as demonstrated that it does work. These are run in the @BeforeClass and @AfterClass in the Cucumber Runner class respectively.

I have also demonstrated that the developed test framework does work by manually running with feature files created on my computer.

What I Want

I want to be able to (eventually) run the automation test framework automatically with our CI tools. Along with this, it would pull the defined automation tests from JIRA and push the test results back to JIRA.

I do not want the feature files stored with the code. In my opinion, this defeats the purpose of it being dynamic as the tests we execute will change over time (in number executed and the steps themselves).

What Is Happening (Or More Specifically, Not Happening)

When I try to execute the Cucumber Runner class without any feature files in the framework, Cucumber says "No features found at [src/test/resources/features/]". This is understandable since there are no feature files (yet).

However, it does not run the @BeforeClass; thus it does not download the feature files to be run. I have tried this both with and without tags in the runner class.

Code

@RunWith(Cucumber.class)
@CucumberOptions( 
        tags={"@smoketests"}, 
        features= {"src/test/resources/features/"},
        plugin={"json:target/reports/cucumber.json"},
        monochrome=true)
public class RunCucumberTest {

    @BeforeClass
    public static void executeBeforeTests() {
        JiraUtil.getFeatureFiles();

        //String browser = "firefox";
        String browser = "chrome";
        //String browser = "safari";
        //String browser = "edge";
        //String browser = "ie";
        DriverUtil.getInstance().setDriver(browser);
    }

    @AfterClass
    public static void executeAfterTests() {
        DriverUtil.getInstance().resetDriver();

        JiraUtil.uploadTestResults();
    }
}

Back To My Question

How can I execute the JIRA Util code so I can download the feature files?

Is it possible to achieve what I want? Or do I have to admit defeat and just have all the feature files stored with the code?


Solution

  • This is the expected behavior when using JUnit. A test suite will not invoke the @BeforeClass, @AfterClass or @ClassRule when there are no tests in the suite or if all tests are ignored[1]. This avoids the execution of a potentially expensive setup for naught.

    This does mean you can't use a class rule to bootstrap your tests. Nor should you attempt to do so. In a build process it is a good practice to fetch all sources and resources prior to compilation.

    If you are using maven could write a maven instead and attach it to the generate-test-sources phase[2]. Creating a maven plugin is a bit more involved then a JUnit Rule but not prohibitively so. Check the Guide to Developing Java Plugins.

    I assume there are similar options for Gradle.