I'm trying to create two tests where one is dependent on the execution of the other one. The project I'm working on is filled with legacy code, so I'm trying to make the main parts of the application testable. The first test will basically try to create some connection to a database and set up some static variables. Test2 will then use the connection and variables to insert some data. I would rather not do the things Test1 does one more time in Test2.
I've made Test2 dependent on test1 so that if Test1 fails, Test2 will not execute. But if Test2 fails I want it to be able to rerun. When i try this in Intellij IDEA I get the following :
java.lang.Throwable: Method a.stack.Test2.failingTest() depends on nonexistent group "FirstTest"
What am I missing?
Test1:
package a.stack;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
/**
* The First test
*/
@Test(groups = {"FirstTest"})
public class Test1 {
public void init(){
// Initialize something which other tests should use
Assert.assertTrue(true);
}
}
And Test2:
package a.stack;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
*
*/
@Test(groups = {"OtherTests"}, dependsOnGroups = {"FirstTest"})
public class Test2 {
public void failingTest(){
Assert.assertTrue(false);
}
}
Testng.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test" verbose="1">
<test name="basic" junit="false">
<groups>
<run>
<include name="FirstTest"/>
<include name="OtherTests"/>
</run>
</groups>
<packages>
<package name="a.*"/>
</packages>
</test>
</suite>
Your group code is incorrect in testng.xml, as it should contains the package name
<groups>
<run>
<include name="packagename.FirstTest"/>
<include name="packagename.OtherTests"/>
</run>
</groups>
then include your classes with package name after group tags [it's optional as you are already using package name]
<class name="packagename.classname1"/>
<class name="packagename.classname2"/>
Code should work now