I have a test class that has a @BeforeClass method and a @Test method. I am running the tests using Ant. I would like to know what is the best way to run only the @BeforeClass method, and not the @Test method. Here is the basic code layout:
@BeforeClass
public void init() throws Exception {
/* Perform test initialization */
}
@Test
public void randomTest() throws Exception {
/* Run the test */
}
I have tried making the @Test method part of a group, and then in Ant specifying the “excludedgroups” property to exclude that group. I have also tried setting the @Test method to be disabled. Neither of these methods worked because it wouldn’t run either the @Test or the @BeforeClass method.
One workaround I have is to make an empty @Test method, and then use either method above, but this does not seem like an elegant solution.
Is there a better way to run only the @BeforeClass method?
Edit:
Here is how I specify the task in my build.xml file:
<testng classpathref="test.classpath"
outputdir="${reports}"
haltonfailure="false"
failureProperty="tests.failed"
useDefaultListeners="true" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter"
parallel="classes"
suitename="Tests"
testname="${test.name} Tests">
I ultimately decided to just put an if statement around every @Test method, and have the statement evaluate a System property.