How to customize the order of execution of tests in TestNG?
For example:
public class Test1 {
@Test
public void test1() {
System.out.printl
An answer with an important explanation:
There are two parameters of "TestNG" who are supposed to determine the order of execution the tests:
@Test(dependsOnGroups= "someGroup")
And:
@Test(dependsOnMethods= "someMethod")
In both cases these functions will depend on the method or group,
But the differences:
In this case:
@Test(dependsOnGroups= "someGroup")
The method will be dependent on the whole group, so it is not necessarily that immediately after the execution of the dependent function, this method will also be executed, but it may occur later in the run and even after other tests run.
It is important to note that in case and there is more than one use within the same set of tests in this parameter, this is a safe recipe for problems, because the dependent methods of the entire set of tests will run first and only then the methods that depend on them.
However, in this case:
@Test(dependsOnMethods= "someMethod")
Even if this parameter is used more than once within the same set of tests, the dependent method will still be executed after the dependent method is executed immediately.
Hope it's clearly and help.
In case you happen to use additional stuff like dependsOnMethods
, you may want to define the entire @Test flow in your testng.xml file. AFAIK, the order defined in your suite XML file (testng.xml) will override all other ordering strategies.
use: preserve-order="true" enabled="true" that would run test cases in the manner in which you have written.
<suite name="Sanity" verbose="1" parallel="" thread-count="">
<test name="Automation" preserve-order="true" enabled="true">
<listeners>
<listener class-name="com.yourtest.testNgListner.RetryListener" />
</listeners>
<parameter name="BrowserName" value="chrome" />
<classes>
<class name="com.yourtest.Suites.InitilizeClass" />
<class name="com.yourtest.Suites.SurveyTestCases" />
<methods>
<include name="valid_Login" />
<include name="verifyManageSurveyPage" />
<include name="verifySurveyDesignerPage" />
<include name="cloneAndDeleteSurvey" />
<include name="createAndDelete_Responses" />
<include name="previewSurvey" />
<include name="verifySurveyLink" />
<include name="verifySurveyResponses" />
<include name="verifySurveyReports" />
</methods>
</classes>
</test>
</suite>
There are ways of executing tests in a given order. Normally though, tests have to be repeatable and independent to guarantee it is testing only the desired functionality and is not dependent on side effects of code outside of what is being tested.
So, to answer your question, you'll need to provide more information such as WHY it is important to run tests in a specific order.
By Specifying test methods to be executed in testNg.xml we can execute the test cases in desired order
<suite>
<test name="selenium1">
<classes>
<class name="com.test.SeleniumTest" >
<methods><include name="methodB"></include>
<include name="methodA"></include>
</methods>
</class>
</classes>
</test>
</suite>
Tests like unit tests? What for? Tests HAVE to be independant, otherwise.... you can not run a test individually. If they are independent, why even interfere? Plus - what is an "order" if you run them in multiple threads on multiple cores?