serenity-bdd / serenity-cucumber

Cucumber integration for the Serenity BDD Reporting library
Other
78 stars 74 forks source link

Serenity Object not available in @BeforeClass annotated method #214

Open sudarsunperu opened 4 years ago

sudarsunperu commented 4 years ago

Hi , I'm trying to do some setup before the Test cases start and i'm using @BeforeClass Annotation in the Runner to do the same. The Runners are executed in Parallel using maven surefire plugin (all run in 1 JVM). The @BeforeClass of each Runner Class takes care of the setup required for its TestCases. I'm trying to save these setup output to Serenity so that my Test cases can directly access them from Serenity. But apparantly The values are not saved to serenity.

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
    features = { "src/test/resources/features/Feature1.feature"}, 
    plugin = {"pretty", "html:target/cucumber", "json:target/cucumber/cucumber-feature1report.json",    "rerun:target/rerun/rerun4.txt"}, 
    glue = {"my.gluecode.packages" }
    )

public class FeatureOneParallelRunner {
    private FeatureOneParallelRunner() {    
    }

    @BeforeClass
    public static void setup() {
        String userName = DataManager.getUserName("Feature1User");
        // I want to save userName to Serenity here
    }

    @AfterClass
    public static void teardown(){
        // Do some teardown stuff here
    }
}

The Pom for the same is

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>2.0.40</serenity.version>
        <serenity.maven.version>2.0.40</serenity.maven.version>
        <serenity.cucumber.version>1.9.31</serenity.cucumber.version>
        <encoding>UTF-8</encoding>
        <tags></tags>
        <parallel.tests>2</parallel.tests>
        <parallel.perCoreThreadCount>false</parallel.perCoreThreadCount>
        <webdriver.base.url></webdriver.base.url>
</properties>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <includes>
            <include>**/*Runner.java</include>
        </includes>
        <parallel>classes</parallel>
        <threadCount>${parallel.tests}</threadCount>
        <forkCount>1</forkCount>
        <perCoreThreadCount>${parallel.perCoreThreadCount}</perCoreThreadCount>
        <systemPropertyVariables>
            <threadCount>${parallel.tests}</threadCount>
            <perCoreThreadCount>${parallel.perCoreThreadCount}</perCoreThreadCount>
        </systemPropertyVariables>
    </configuration>
</plugin>

It uses JUnit 4.12 Please let me know if you need any more details.

wakaleo commented 4 years ago

I doubt this will be possible because of the JUnit lifecycle order.