Method depends on nonexistent group - Testng
Asked Answered
L

4

9

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>
Lysimachus answered 16/2, 2012 at 13:54 Comment(0)
D
1

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

Decennium answered 22/8, 2018 at 10:25 Comment(0)
V
1

An alternate way that always works out is to run tests from the package instead of a specific class using the green carrot sign at the left. This is a common miss while running depends on dependsOnGroups.

enter image description here

Venal answered 7/9, 2020 at 2:2 Comment(0)
W
0

Are you sure that the package you specify contains methods in that group?

Willner answered 17/2, 2012 at 0:0 Comment(2)
It works on the first run, but when Test2 fails and I do a rerun of the failed tests it throws the throwable which indicates that the group, FirstTest, does not exist.Lysimachus
Hello Cedric, perhaps you should reword this answer to be more of a statement rather than a question.Lorraine
I
0

If a test fails in a suite then a testng-failed.xml file is created in the output directory which is used to rerun the failed cases. Can you please check that file to make sure that the xml file contains both the groups and not just OtherTests, which actually failed ?

 <run>
      <include name="FirstTest"/>
      <include name="OtherTests"/>
 </run>

Because If it doesn't have the group FirstTest then the error depends on nonexistent group "FirstTest" is thrown.

Idola answered 16/10, 2016 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.