akiran423
akiran423

Reputation: 11

how to keep dependency in testng in different classes webdriver

I have a scenario like two different test classes, second test class result depends on the first one

public class class1 { @test public void sometest1() { // some functionality } }

public class class2
{
    @test
    public void sometest2()
    {
        // some different functionality
    }
}

Here class2 need to be executed if class1 result is pass else class2 needs to be failed.(if no provision for the same i need to execute the test1 in class1 to be executed always first in selenium grid parallel run)

Upvotes: 1

Views: 8597

Answers (4)

murali selenium
murali selenium

Reputation: 3927

As said avoid please refer :http://testng.org/doc/documentation-main.html#annotations.

As per above comments, i hope you are trying use depends on for method which is in another class. In such a case you need to provide complete path ie package.class.method.

class1:

public class testingcode {


@Test
public void test1(){
    System.out.println("test1");
}
}

Class2:

public class testcase1{

@Test(dependsOnMethods={"com.test.testingcode.test1"})
public void testcase1result() throws InterruptedException{

    System.out.println("test");
}
}

Run from TestNG.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none" preserve-order="true">
<test name="Test">
<classes>
  <class name="com.test.testingcode"/>
  <class name="com.test.testcase1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Let me know, if it is not working..

Thank You, Murali

Upvotes: 2

Shineed Basheer
Shineed Basheer

Reputation: 578

You can use dependsOnMethods like below

   @Test
   public void test1() {
       // Instantiate another case class 
       // Call your testcase
   }      

  @Test(dependsOnMethods="test1")
   public void test2() {
   }

Upvotes: 0

barunsthakur
barunsthakur

Reputation: 1216

You can use groups and dependency in testng to run a test after some test have executed. There is no way(atleast to my knowledge) to fail the Class2 test if Class1 test failed.

public class Class1Test {
    @Test(groups="RunFirst")
    public void sometest1() {
        Assert.fail("just to skip the other test");
    }
}

public class Class2Test {
    @Test(dependsOnGroups="RunFirst")
    public void sometest2() {
        System.out.println("I am running");
    }
}

Here the Class2Test will run after Class1Test has run and will be skipped if Class1Test failed.

Upvotes: 0

0o&#39;-Varun-&#39;o0
0o&#39;-Varun-&#39;o0

Reputation: 735

There are some annotations in testng that allows you the flow that you want. I have never worked on testng but I think you can find the suitable annotation here :http://testng.org/doc/documentation-main.html#annotations

might be dependsOnGroups help you

Upvotes: 0

Related Questions