TestNG dependsOnMethods from different class
Asked Answered
P

5

32

The dependsOnMethods attribute of the @Test annotation works fine when the test to be depended upon is in the same class as that of the test that has this annotation. But it does not work if the to-be-tested method and depended-upon method are in different classes. Example is as follows:

class c1 {
  @Test
  public void verifyConfig() {
    //verify some test config parameters
  }
}

class c2 {
  @Test(dependsOnMethods={"c1.verifyConfig"})
  public void dotest() {
    //Actual test
  }
}

Is there any way to get around this limitation? One easy way out is to create a test in class c2 that calls c1.verifyConfig(). But this would be too much repetition.

Patience answered 7/10, 2011 at 19:42 Comment(0)
K
40

Put the method in a group and use dependsOnGroups.

class c1 {
  @Test(groups={"c1.verifyConfig"})
  public void verifyConfig() {
    //verify some test config parameters
  }
}

class c2 {
  @Test(dependsOnGroups={"c1.verifyConfig"})
  public void dotest() {
    //Actual test
  }
}

It is recommended to verify configuration in a @Before* and throw if something goes wrong there so the tests won't run. This way the tests can focus on just testing.

class c2 {
  @BeforeClass
  public static void verifyConfig() {
    //verify some test config parameters
    //Usually just throw exceptions
    //Assert statements will work
  }

  @Test
  public void dotest() {
    //Actual test
  }
}
Knockwurst answered 7/10, 2011 at 23:50 Comment(3)
This is working fine when running the the class file individual while running it through testing.xml file getting error DependencyMap::Method "LoanApprovalTest.testLoanApprova(java.util.Hashtable)[pri:0, instance:com.zions.release1.Sanity.LoanTestCases.LoanApprovalTest@3884b2]" depends on nonexistent group "CreateLoanAccountTest.testCreateLoanAccount"Pederson
@ArpanSaini Both classes need to be under the same test.Atmospheric
Is writing the class name in the group name (c1) necessary? What if I want to club test methods from different classes in one group?Extortion
H
4

DependsOnMethods cannot be used from different class ,To resolve this we can use dependsOnGroups;

Do code change in;

1. dependsOnGroups class;

@Test(groups={"prerequisites" })

public void M1()
{

}

2. class which calls dependsOnGroups;

@Test(dependsOnGroups={"prerequisites"})
public void M2()

{

}

3. xml

<groups>
    <run>
        <include name ="prerequisites"/>
    </run>
</groups>
Hochheimer answered 23/8, 2018 at 3:54 Comment(0)
A
4

You can use groups and dependsOnGroups in the TestNG @Test annotation, as described in earlier answers.

However, both classes need to be under the same <test>.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite" verbose="1" >
  <test name="Test" >
    <classes>
       <class name="c1" />
       <class name="c2" />
    </classes>
  </test>
</suite>

The following will result in an exception when running the test suite.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" >
  <test name="Test1" >
    <classes>
       <class name="c1" />
    </classes>
  </test>

  <test name="Test2">
    <classes>
      <class name="c2" />
    </classes>
  </test>
</suite>
Atmospheric answered 29/8, 2018 at 7:18 Comment(0)
H
1

Two solutions: 1. Using dependsOnGroups and inheritance

    import static org.testng.Assert.fail;
    import org.testng.annotations.Test;

    public class PTest1 {
        @Test(groups = "A")
        public void test11() {
            System.out.println("test11");
            fail();
        }
    }


    import org.testng.annotations.Test;
    public class PTest2 extends PTest1 {

        @Test(groups = "B", dependsOnGroups = "A")
        public void test21() {
            System.out.println("test21");
        }
    }


    <suite name="priority" verbose="1">
        <groups>
            <run>
                <include name ="B"/>
            </run>
        </groups>
        <test name="pri2">
            <classes>            
                <class name="priority.PTest2"/>
            </classes>
        </test>
        <test name="pri1">
            <classes>            
                <class name="priority.PTest1"/>
            </classes>
        </test>    
    </suite>
  1. Use programming:

    import static org.testng.Assert.fail;
    import org.testng.annotations.Test;
    public class PTest3 {
    
        @Test
        public void test31() {
            System.out.println("test31");
            fail();
        }
    }
    
    import org.testng.IInvokedMethod;
    import org.testng.ITestContext;
    import org.testng.SkipException;
    import org.testng.annotations.Test;
    
    public class PTest4 {
    
        @Test
        public void test41(ITestContext context) {
            for (IInvokedMethod iInvokedMethod : context.getSuite().getAllInvokedMethods()) {
                if (iInvokedMethod.getTestMethod().getMethodName().equals("test31")
                        && !iInvokedMethod.getTestResult().isSuccess()) {
                    throw new SkipException("test31 is not sucessful!");
                }
            }
            System.out.println("test41");
        }
    }
    
    <suite name="priority" verbose="1">
        <test name="pri3">
            <classes>            
                <class name="priority.PTest3"/>
            </classes>
        </test>
        <test name="pri4">
            <classes>            
                <class name="priority.PTest4"/>
            </classes>
        </test>    
    </suite>
    
Hoye answered 28/4, 2020 at 3:24 Comment(0)
L
0

While using depends on group it does not generate correct testng-failed.xml For example if class A m1 is dependent by class B m2 method and if m2 gets failed, testng-failed should consider both class A m1 and class B m2 method in order to re run failed test cases but it's not considering m1 method in testNG-failed.xml. Same thing is possible with depends on method.

Listing answered 4/7 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.