@BeforeSuite is not called when testing one class

I have a method @BeforeSuite

called -annotated.

public class MySuiteTest {

    @BeforeSuite
    public static void doSomethingVeryMandatory() {
        // say, boot up an embedded database
        // and create tables for JPA-annotated classes?
    }
}

public class MySingleTest {

    @Test
    public void doSomething() {
        // say, tests some MyBatis mappers against the embedded database?
    }
}

      

When I test the whole test,

$ mvn clean test

      

all perfectly. @BeforeSuite

executed and @Test

executed.

When I tried to test one class

$ mvn -Dtest=MySingleTest clean test

      

doSomethingVeryMandatory()

is not called.

This is normal?

+5


source to share


2 answers


Your @BeforeSuite and @Test are in different classes. When you run one class, testng creates a standard suite.xml package with one class. Hence your @BeforeSuite doesn't show up for testing. You can either extend MySuiteClass in your test class or create a suite file and run the package file as suggested in the comments.



+2


source


Babule's comment on the question works great, just giving examples for future readers:

  1. Create a set in XML TestNG configuration file:
<suite name="Suite Name" verbose="0">
    <test name="TestName">
        <classes>
            <class name="MySuiteTest"/>
            <class name="MySingleTest"/>
        </classes>
    </test>
</suite>

      



  1. Calling this config file using maven in pom.xml
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>config/testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
</plugin>

      

0


source







All Articles