How to execute testNG methods in order?

10,535 views
Skip to first unread message

Naveen

unread,
Jul 5, 2012, 12:03:19 AM7/5/12
to testng-users
Hi guys!
I am trying to use testNG to generate reports, while executing
the class the methods are not executed sequentially as executed in the
Junit. And i dont want to use dependsonMethod
My order is like(i want to execute as i written in the class)..
public class Views {
@Test
public void b(){...}
@Test
public void a(){...}
@Test
public void c(){...} .... }
the o/p should be b,a,c but my output is a,b,c it is executing in
alphabetical order.
Please help me to overcome this problem....

吴亭

unread,
Jul 5, 2012, 1:13:27 AM7/5/12
to testng...@googlegroups.com
Hi,

You need add preserver-order=true in your testng.xml

Br,
Tim

2012/7/5 Naveen <naveen...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Cédric Beust ♔

unread,
Jul 5, 2012, 1:23:15 AM7/5/12
to testng...@googlegroups.com
Note that 1) this is the default and 2) this only applies to the ordering specified in the XML file, so not applicable here.

The ordering of methods in the class file is unpredictable, so you need to either use dependencies or include your methods explicitly in XML.

-- 
Cédric

Abhay

unread,
Jul 5, 2012, 2:57:58 AM7/5/12
to testng...@googlegroups.com
Hi,
we can also do by setting Priority.
Cedric if I am wrong please correct me.
Thanks,
Abhay

-- 
Cédric




Tim


2012/7/5 Naveen <naveen...@gmail.com>
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

Cédric Beust ♔

unread,
Jul 5, 2012, 3:02:09 AM7/5/12
to testng...@googlegroups.com
No, you're right, priorities work too.

-- 
Cédric




To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/NmN0fVqPkG4J.

To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

Abhay

unread,
Jul 5, 2012, 3:26:03 AM7/5/12
to testng...@googlegroups.com
Thanks alot Cedric 
Abhay

Amit Joshi

unread,
Aug 7, 2013, 9:28:56 AM8/7/13
to testng...@googlegroups.com
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
  <test verbose="2" name="Default test">
       <classes>
  
      <class name="Login.Login1">
     
        <methods>
            <include name="b"/>
            <include name="a"/>
            <include name="c"/>
            <include name="d"/>
            <include name="e"/>
            <include name="Date"/>
            <include name="Submit"/>
            <!--  <include name="Suspect"/>-->
        </methods>
        </class>
       <!-- TmrsLogin.TmrsLogin1 -->
    </classes>
  </test> <!-- Default test -->
</suite> <!-- Default suite -->

What ever order u want. Can be set in XML file according to requirements.

Radha Krishna

unread,
Aug 23, 2013, 8:58:33 AM8/23/13
to testng...@googlegroups.com, ced...@beust.com

Hi Cedric,

 I have ClassOne with 4 test methods, ClassTwo with 3 test methods. The order of test methods in ClassOne is m1(), m2(), m3(), m4() and in ClassTwo is t1(), t2(), t3().

<suite name ="Suite1">
<test  name="Running Tests">
        <classes>
            <class name="testNG.Parallel.ClassOne " />
            <class name="testNG.Parallel.ClassTwo " />
        </classes>
    </test>
</suite>

When I run this xml file, do the test methods in the above classes get executed/run in the same order as I wrote in the class.


On Thursday, 5 July 2012 10:53:15 UTC+5:30, Cédric Beust ♔ wrote:

Krishnan Mahadevan

unread,
Aug 23, 2013, 10:38:47 AM8/23/13
to testng...@googlegroups.com
No they wouldnt.

As Cedric already mentioned, either use dependsOnMethods (or) call out your method names for every class

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

talon

unread,
Aug 29, 2013, 2:12:44 PM8/29/13
to testng...@googlegroups.com, ced...@beust.com
if you want them in the same order you can do this

@Test(priority = 1) ----> for m1()

@Test(priority = 2) ----> for m2() 

and so on..

Sanchayan Paul

unread,
Apr 21, 2014, 5:34:36 AM4/21/14
to testng...@googlegroups.com
Hi Krishnan,

My xml looks like this - 

<suite name="ALPHA SMOKE SCRIPTS" thread-count="5" verbose="10" preserve-order="classes" >
<tests>
<test name="Smoke_Room_Type_Configuration_001" description="Verify  the  Managed Column in the Room Type  screen for ACTIVE Type as RMS Support Users">
<parameter name="scenario" value="SMOKE SCRIPTS" />
<parameter name="testcase" value="Smoke_Room_Type_Configuration_001" />
<parameter name="browser" value="firefox" />
<parameter name="description" value="Verify the Managed Column in the Room Type screen for ACTIVE Type as RMS Support Users" />
<classes preserve-order="true" preserver-order="classes">
<class name="components.Common">
<methods>
<include name="login"/>
<include name="propPINChange"/>
</methods>
</class>
<class name="components.RoomTypeConfigComp">
<methods>
<include name="navigateToRoomClassViaDpDwn"/>
<include name="verifyRoomTypesTable"/>
</methods>
</class>
</classes>
</test>
</tests>
</suite>

Problem
-- the methods in class RoomTypeConfigComp (which has been declared later in the testng xml) are getting executed prior to the methods in the class Common(which has been declared at first as per as sequence is concerned).

I have tried out with preserve-order, but it did not work. Could you pleae help me out in this case?. Its very urgent

Thanks and Regards
Sanchayan

Krishnan Mahadevan

unread,
Apr 21, 2014, 12:47:59 PM4/21/14
to testng...@googlegroups.com
Can you please try with : preserve-order=true parallel=false and see that helps ? [ set this at the <test> tag level ]

TestNG honours preserve-order=true attribute only when parallelism has been explicitly disabled. 

~ Krishnan

iSent. iPhone. iReget.iTypos!
For more options, visit https://groups.google.com/d/optout.

Sanchayan Paul

unread,
Apr 22, 2014, 1:29:01 AM4/22/14
to testng...@googlegroups.com
Thanks for your suggestion Krishnan, but it did not work.
Anyways, i will anyways have to run the suite in parallel mode.

it seems that it is picking up the class names in reverse alphabetical order (since R comes after C)

Krishnan Mahadevan

unread,
Apr 22, 2014, 1:53:53 AM4/22/14
to testng...@googlegroups.com
I am not entirely sure what exactly did you try. Here's what worked for me [ Am assuming that this is what you need ]

My Suite XML file looks like below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test" parallel="false" preserve-order="true" verbose="2">
<classes>
<class name="org.rationale.emotions.testng.Common">
<methods>
<include name="login" />
<include name="propPINChange" />
</methods>
</class>
<class name="org.rationale.emotions.testng.RoomTypeConfigComp">
<methods>
<include name="navigateToRoomClassViaDpDwn" />
<include name="verifyRoomTypesTable" />
</methods>
</class>
</classes>
</test>
</suite>

Here's the output


org.rationale.emotions.testng.Common.login() is running on Thread 1
User logged in
org.rationale.emotions.testng.Common.propPINChange() is running on Thread 1
User's pin was changed
org.rationale.emotions.testng.RoomTypeConfigComp.navigateToRoomClassViaDpDwn() is running on Thread 1
User navigated to Room via drop down
org.rationale.emotions.testng.RoomTypeConfigComp.verifyRoomTypesTable() is running on Thread 1
Room types verified.
PASSED: login
PASSED: propPINChange
PASSED: navigateToRoomClassViaDpDwn
PASSED: verifyRoomTypesTable

===============================================
    Test
    Tests run: 4, Failures: 0, Skips: 0
===============================================










Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Sanchayan Paul

unread,
Apr 22, 2014, 4:25:47 AM4/22/14
to testng...@googlegroups.com
Thanks Krishnan, It is following the order now.
But my objective is to run this king of series of tests in parallel mode? Also, the problem that I am now getting is - the driver object is getting created in @BeforeTest for each test, and the class in which all these configuration related annotations are have been extended in all the sub classes (e.g., Common, RoomTypeConfigComp). but when I am trying to run testng xml, the driver object is becoming null in the 2nd method which is in the class RoomTypeConfigComp

Could you please help me.

Krishnan Mahadevan

unread,
Apr 22, 2014, 6:05:24 AM4/22/14
to testng...@googlegroups.com
There are a lot of things that I dont know here about your project, so I dont know how much I can help.

if ordering is to be maintained via preserve-order=true, you would need to disable parallelism. Else it is NOT going to work.

If you would like your classes to run in parallel but the methods in each of the classes has to follow an order, then you should be using dependsOnMethods (or) dependsOnGroups to chain your tests.




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Sanchayan Paul

unread,
Apr 22, 2014, 6:49:34 AM4/22/14
to testng...@googlegroups.com
Let me try to explain - 

1. there is a driver class called InitTest.java, which actually serves as the controlling class. This has - 
      a. a public webdriver variable called driver
      b. a public connection object
      c. a public properties file object
      
2. Following it, there are several classes, which has methods in those and which i would like to run as per the xml. 

Problem
1. I have used public class RoomTypeConfigComp extends InitTest and public class Common extends InitTest
2. Once the control goes from Common (after executing Login) to RoomTypeConfigComp, all the public variables are becoming null

Let me know if you want any other info.

Krishnan Mahadevan

unread,
Apr 22, 2014, 7:46:28 AM4/22/14
to testng...@googlegroups.com
Replace the @BeforeTest in your base class with @BeforeClass and see if that helps !

Basically the @BeforeTest executes only once per <test>. In your case, both the classes are extending from a common base class and the initialization is being done in the @BeforeTest. I am guessing that when it comes to the second instance, its initialization is NOT happening at all, because TestNG detects a @BeforeTest method which it has already executed and wont execute once again. Net Result : NullPointerException.

Resorting to @BeforeClass ensures that TestNG will invoke it for every test class.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Sanc

unread,
Apr 22, 2014, 8:03:33 AM4/22/14
to testng...@googlegroups.com
Thanks for your useful advice.

But don't you think that in my case if I go by @BeforeClass, then with two classes, I will have two separate browsers (for a single test)

Krishnan Mahadevan

unread,
Apr 22, 2014, 8:25:10 AM4/22/14
to testng...@googlegroups.com
That was exactly the idea. If you are basically thinking of creating one browser instance for an entire <test> I am not sure as to what is the effectiveness of this gigantor test. You should strive to keep test classes modular as much as possible rather than weave in so much of dependencies etc., 

That's my personal opinion !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Sanchayanpaul Paul

unread,
Apr 22, 2014, 8:32:07 AM4/22/14
to testng...@googlegroups.com
yeah. because each test comprises of test methods distributed in two or more classes. My idea was to create a browser instance for each test and close it and then go onto the next one. then the bigger picture is to run these tests parallely. Please suggest!!

Sent from my iPhone
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/nKJQExG7yk4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Apr 22, 2014, 8:41:00 AM4/22/14
to testng...@googlegroups.com
If you would like browsers to be created ONLY for every <test> tag rather than for every test class, then I would suggest that you please resort to creating browser instances via TestNG listeners. You would begin by creating a listener that implements org.testng.ITestListener and then within org.testng.ITestListener.onStart(ITestContext) you would incorporate the logic of creating the browser and then push this value into a Map which perhaps looks like this :

Map<String, WebDriver> browserMaps [ here key is the name of the <test> tag ] we can safely use the <test> tag's name because TestNG doesn't let you define two <test> with the same name.

Now each of your test methods would basically query the browser instance by passing its <test> name [ this can be conveniently retrieved via Reporter.getCurrentTestResult().getTestContext().getName() ]

and within the org.testng.ITestListener.onFinish(ITestContext) of your listener you would incorporate the logic of closing the already opened browser.

That should get past your problem !







Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Sanchayan Paul

unread,
Apr 22, 2014, 9:00:51 AM4/22/14
to testng...@googlegroups.com
Thanks Kishnan.. Let me give it a try
Thanks $ Regards
Sanchayan

Sanchayan Paul

unread,
Apr 23, 2014, 1:45:12 AM4/23/14
to testng...@googlegroups.com
Hi Krishnan,

Considering what you have suggested above, I have two more requirements - 
1. Parallel run of the tests
2. Each method may run more than once (is there any way of mentioning this with any attribute in the testng xml?)

Are these two possible also. Please let me know, as i need to go ahead with these.

Thanks


On Tue, Apr 22, 2014 at 6:11 PM, Krishnan Mahadevan <krishnan.ma...@gmail.com> wrote:



--
Thanks $ Regards
Sanchayan

Krishnan Mahadevan

unread,
Apr 23, 2014, 2:02:43 AM4/23/14
to testng...@googlegroups.com
I believe that we are gradually hijacking this thread's context!

So this is going to be my last response on this thread because I believe I have answered the original question that you had !

Yes, parallel tests can be executed by the approach I highlighted before. [ It wouldnt hurt for you to play around and see it for yourself ]

>>>Each method may run more than once (is there any way of mentioning this with any attribute in the testng xml?)

To the best of my knowledge, this is NOT something that you can do via the testng xml approach.

You would either need to use :

a. Simple dataprovider powered test method (or)
b. A factory driven by a data provider wherein each test class has this chaining defined in it.




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Sanchayan Paul

unread,
Apr 23, 2014, 2:06:54 AM4/23/14
to testng...@googlegroups.com
Thank you very much for the ideas.

siddhant raut

unread,
Apr 27, 2017, 9:30:19 AM4/27/17
to testng-users
Hi Naveen,

I found this issue because i was using testNG lates dependency 6.11 i change it to 6.9.10 and works successfully.


please try and let me know if it works

Mitul Thesiya

unread,
Apr 28, 2017, 10:55:32 AM4/28/17
to testng-users
I'm using currently testng 6.11 version jar and found below obervation.

1. When you are giving no priority then, all test methods will be executed by natural sorting order of testNG that is ... sorted by TestMethod alphabetically in descending order.

2. When you will give any priority, test methods having same priority in same module will be behave similarly point 1.

3. When more than two class you define then, all test methods will be executed with respect to its priority/or default priority regardless of that class. Point 1 is also impacted here for test methods in alphabetical order, but of each individual class (test method order by alphabetical of classA... -> and then test method order by alphabetical of class B)
i.e. class A-> 10 test methods. (5 test method with 0 priority, 5 test method with 1 priority)
      class B-> 2 test method (2 test method has default priority, means 0)

Now in execution order I measured as following:
   3.1 Class A-> 5 test method having 0 priority will be executed.
   3.2 Class B-> 2 test method with having default prority(0 here) will be executed.
   3.3 Class A-> rest 5 test method having priority 1 will be executed.

I've also tried with giving 0 priority(although its similar to default priority 0) for class B methods, but it behaves similar. 
Reply all
Reply to author
Forward
0 new messages