Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute all the methods sequentially in testng

I have many methods in my class , when i run the code ,methods are called randomly , but in my class ,every method depends on its predeccessor ,ie 2nd method depends on the 1st method , 3rd method depends on the 2nd method and so on .I want to exectue all the methods sequentially

I have tried the below metods and tested the code ,but still the methods are called randomly

//using sequential
@Test(sequential = true)
public void Method1(){
}


@Test(sequential = true)
public void Method2(){
}

//using singleThreaded 
@Test(singleThreaded=true)
public void Method1(){
}


@Test(singleThreaded=true)
public void Method2(){
}

I have passed the following parameter in the testng as well

<test name="Test" preserve-order="true" annotations="JDK">
 <classes>
 <class name="com.test" >
 <methods>
 <include name="method1"/>
 <include name="method2"/>
 <include name="method3"/>...
 </methods>
 </class>
  </classes>

 </test>

 </suite>

When I tested it with @Test(dependsOnMethod="") ,instead of executing the methods sequentially, the methods are getting skipped.

How to execute the test sequentially in testng?

like image 988
Vignesh Avatar asked Oct 03 '13 09:10

Vignesh


People also ask

How do you run test cases sequentially in TestNG?

parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread. parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

How the methods run in order in TestNG?

TestNG executes different tests alphabetically. By default, test1 will run first and after that test2 and finally test3. By default, TestNG assigns priority as 0 to all tests if priority is not defined by the user. Since all tests are having same priority, it executes in an alphabetic order.


3 Answers

Dependsonmethods will make your tests as skipped if the method they depend on fails. This is logically correct since if testB depends on testA then if testA fails, then there's no use of running testB. If all you need is testB runs after testA and testB doesn't depend on the result of testA, then add a alwaysrun=true to your @test annotation. These are known as. Soft dependencies. Refer here.

like image 31
niharika_neo Avatar answered Oct 27 '22 00:10

niharika_neo


if you want to run your all test method in some specific manner, than just add priority in your @test annotation. see following:-

@test(priority=0)

function1()
{}

@test(priority=1)

function2()

{}


@test(priority=5)

function3()

{}

@test(priority=3)

function4()

{}

@test(priority=2)

function5()

{}

In this case function1 call first than function2, after function 5 will call instead of function3 because of priority.

like image 152
Naveen Chhaniwal Avatar answered Oct 26 '22 23:10

Naveen Chhaniwal


First off, you don't need the sequential or singleThreaded parameters. Writing the single methods in the suite is all you need. Your problem probably lies somewhere else. Make sure you're starting the test using your suite, not the class itself.

In case you don't want to use suites every time (because it's tedious, error-prone and unflexible) here are a few solutions to this problem.

  1. Put dependent methods into one method and make excessive use of Reporter.log(String, boolean). This is similar to System.out.println(String), but also saves the String to TestNG reports. For the second argument you always want to pass true - it tells whether the message should also be printed to STDOUT. When doing this before every step, the test output alone should be enough to identify problematic (read failing) test steps.

    Additionally when doing this you have the possibility of using Soft Assertsions. This basically means that you don't have to abort and fail a whole test only because one optional step doesn't work. You can continue until next critical point and abort then or at the end. The errors will still be saved and you can decide whether you want to mark a test run as failed or unstable.

  2. Use @Test(priority = X) where X is a number. Mark all your test methods with a priority and they'll be executed in the order of the priority number from lowest to highest. The upside of this is that you can put methods inbetween steps and the single annotations are independent. The downside however is that this doesn't force any hard dependencies, but only the order. I.e. if method testA with priority=1 fails, method testB with priority=2 will be executed nonetheless.

    You probably could work around this using a listener though. Haven't tried this yet.

  3. Use @Test(dependsOnMethods = {"testA"}). Note that the argument here is not a string, but a list of strings (you have that wrong in your post). The upside is hard dependencies, meaning when testB depends on testA, a failure of testA marks testB as skipped. The downside of this annotation is that you have to put all your methods in a very strict chain where every method depends on one other method. If you break this chain, e.g. having multiple methods not depend on anything or having some methods depend on same methods, you'll get into hell's kitchen...

Using both priority and dependsOnMethods together doesn't get you where you'd expect it to unfortunately. Priority is mostly ignored when using hard dependencies.

like image 29
Hubert Grzeskowiak Avatar answered Oct 27 '22 00:10

Hubert Grzeskowiak