Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG dependsOnMethods from different class

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.

like image 368
brayne Avatar asked Oct 07 '11 19:10

brayne


People also ask

Can we make a test dependent on another in TestNG?

TestNG lets you create dependencies between groups in the XML file. So, if you have multiple groups in the TestNG file, you can create the dependent tests in between them in the XML file.

How do you call one test from another test in TestNG?

A test method can have different-different test properties and test attribute with test data as well. In your case : addAppointment addApp = new addAppointment(); addApp. testaddAppointment();


3 Answers

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   } } 
like image 82
Cedric Beust Avatar answered Sep 18 '22 19:09

Cedric Beust


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> 
like image 34
RANA DINESH Avatar answered Sep 20 '22 19:09

RANA DINESH


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>
like image 21
Samer Makary Avatar answered Sep 20 '22 19:09

Samer Makary