I have a testng suite file which has 10 classes inside a test. Say
<classes>
<class name = "Class1">
<class name = "Class2">
<class name = "Class3">
</classes>
Now class1 has 3 methods with priority 1,2,3 in order and the same with class 2 and class3. When I run the job in Jenkins with suite having preserve-order="true", the tests run as class1-priority1 test, class2-priority1, class3-priority1, class1-priority2, class2-priority2 etc.
I need to have all the tests associated with class1 executed inorder first and then class2 and then class3.
Could someone tell me why this behavior is?
The default order depends on the Java reflection API. You may include method names under < methods > in the order you want. This may be cumbersome & may look clumsy while file gets bigger. Nevertheless, it may help you in this regard.
Example xml:
<class name="Fully qualified class name without extension">
<methods>
<include name="method_1" />
<include name="method_1" />
.....
.....
<include name="method_N" />
</methods>
</class>
Other than the previous solution, you may add dependency for test cases in your test methods using annotation. But, changing/modifying xml will far better than modifying codes.
The order you describe is the expected one.
Instead of priority, you should try to use dependsOnMethods.
Related
I want to run a specific test class before running other tests in the same package using testng xml.
test_package
-------------
testclass1
testclass2
testclass3
I want testclass3 to run first.
Selenium grid is being used. Currently the longer test runs at last and other nodes are idle making the test duration longer. How can i give first priority to longer test classes.
<test name="Tests">
<packages>
<package name="tests.*"/>
</packages>
</test>-->
There are multiple classes inside the package but i want selected test class to run ahead of all.
If you want to run the classes in order, you can specify the order in the xml as given below, By default testng, run the classes in order mentioned in xml.
<test name="Tests" preserve-order="true">
<classes>
<class name="tests.testclass3"/>
<class name="tests.testclass1"/>
<class name="tests.testclass2" />
</classes>
</test>
If you include
<package name="tests.*"/>
Then looks there is no way to say order of classes to execute.
You can try by providing classes with preserve order is testng.xml file or you can create xml file by coding like here.
i have a testNG xml which will execute a (one)test case 2 times. the difference between each time is the test level parameter. so i have to execute the test case with 2 different parameter (2 user).
Now i will be creating new xml (suite of xml) from which i will call the existing xml. i will define a new parameter in suite xml for the User.
Expectation is if suite xml parameter="user2", then the child xml should execute the test case only once which has parameter as user2.
i tried beanshell scripting and found it useful for method-selector. but i want to make decision for test level and not method level.
Below is the testNG.xml which calls the test case 2 times with different user value. TestCase will be called first time with User="USER1" and second time with User="USER2".
<?xml version="1.0"?>
<suite name="TestLoad">
<test verbose="10" name="TestForUser1" preserve-order="true">
<parameter name="User" value="USER1"/>
<classes>
<class name="com.dummy.test.TestCase"/>
</classes>
</test>
<test verbose="10" name="TestForUser2" preserve-order="true">
<parameter name="User" value="USER2"/>
<classes>
<class name="com.dummy.test.TestCase"/>
</classes>
</test>
</suite>
Below is the Suite of xml which i will be newly creating to call many testng.xml described as above.
<?xml version="1.0"?>
<suite name="suiteOfXml">
<parameter name="User" value="USER1"/>
<suite-files>
<suite-file path="TestLoad.xml"/>
<suite-file path="TestStage.xml"/>
</suite-files>
</suite>
Expectation is something like:
if the suiteOfXml has User="USER1" then each testNG xml should run the TestCase only once with User=USER1.
if the suiteOfXml has User="USER2" then each testNG xml should run the TestCase only once with User=USER2.
if the suiteOfXml has User="ALL" then each testNG xml should run the TestCase twice. once with User=USER1 and next time with User=USER2.
I cannot make any changes to the TestCase (java class level). condition should be made at xml only.
Kindly provide a possible solution. Thanks in advance
Here's what you need to be doing to get past this issue (You dont need a suite of suites etc., )
Create an implementation of the TestNG listener interface org.testng.IAlterSuiteListener within which you basically read the value of the parameter and then based on it, you decide which <test> should be created and how it would look like etc.,
You create a suite xml file that only contains the parameter which would help you decide what <test> should be there and also a reference to the newly created listener from the previous step.
That should do. Using this approach you can basically do all the things that you have listed out.
Here's how a sample listener would look like (In this example, we are getting the packages to be executed via a JVM argument and then based on that, we are building our <test> tag)
package com.rationaleemotions.wordpress;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.Collections;
import java.util.List;
public class SimpleSuiteAlterer implements IAlterSuiteListener {
#Override
public void alter(List suites) {
XmlSuite suite = suites.get(0);
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName("CommandLine_Test");
String packages = System.getProperty("package", suite.getParameter("package"));
XmlPackage xmlPackage = new XmlPackage(packages);
xmlTest.setXmlPackages(Collections.singletonList(xmlPackage));
}
}
And here's how the suite xml would look like
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package-Suites" verbose="2">
<parameter name="package" value="com.rationaleemotions"/>
<listeners>
<listener class-name="com.rationaleemotions.wordpress.SimpleSuiteAlterer"/>
</listeners>
</suite>
For a more detailed explanation and some code samples, refer to my blog post https://rationaleemotions.com/building_dynamic_testng_suites/
Below is my project structure in eclipse :
->testclasses
---->AccountTest(Priority of Methods from 1-6)
---->BillingTest(Priority of Methods from 7-13)
---->HomePageTest(Priority of the only method is 17)
---->SupportTest (Priority of Methods from 14-16)
All the test classes above have methods where priority is set in incremental order as shown above.
Now when I right click on testclasses package and run it as Testng. It starts the execution with HomePageTest.
I am setting the priority of my test methods as below :
#Test(priority=6, dataProvider="Setup")
I want the execution to be as per the priority defined for each method and Thus method with priority 1 should execute first irrespective of which class it is in.
The correct way to run all the tests present in multiple classes with priorities is to run those by a testng file. So write all the classes name in the testng.xml file and then run the testng file by right clicking on it from the package explorer--> Run As-->TestNG Suite. Your test cases will run according to the priorities set irrespective of the classes they belong to.
You testng.xml should look like:
<test name="TestSuiteName">
<classes>
//Insert the whole path of the classes here like
<class name="packageName.AccountTest" />
<class name="packageName.BillingTest" />
<class name="packageName.HomePageTest" />
<class name="packageName.SupportTest" />
</classes>
</test>
TestNG runs test cases in order of the priorities. If there is no priority for a testmethod then by default TestNG sets the priority to 0. In your case, in HomePageTest class there may be some method with priority 0. Either set priority of all the methods or use test methods in xml runner file to run in a given order
<test name="DummyTest">
<classes>
<class name="apitestset.inventory.Test">
<methods>
<include name="create"/>
<include name="update"/>
<include name="get"/>
<include name="check"/>
<include name="initiate"/>
<include name="confirm"/>
<include name="extend"/>
</methods>
</class>
<classes>
Here testclass has 7 methods and they run in the order given in xml runner file. Don't set the priority if you are using methods in xml runner file.
I'm doing Selenium tests in TestNG. I would like to not have the maintenance annoyance of keeping all of my test methods in a separate file and would rather just include a package to scan for #Tests.
Given this scenario, is there a way to preserve the order of the test methods within the test file AND have TestNG consume one test class at a time. Currently, I am maintaining the method order within the tests with #Test(dependsOn={}) which works; however, I'm seeing this situation
TestClassA
test_method_1
test_method_2
test_method_3
test_method_4
TestClassB
test_method_1
test_method_2
test_method_3
test_method_4
Run tests and the order is something like:
ClassB, method 1
ClassA, method 1
ClassB, method 2
ClassB, method 3
ClassA, method 2
ClassA, method 3
ClassB, method 4
ClassA, method 4
I don't care about the Class order, I only care that the methods of one Test Class are run entirely before moving on to the next Test Class.
Anyone?
It would be great to see your testng.xml file. It seems that similar situation is described here: http://testng.org/doc/documentation-main.html#testng-xml. I think in your case you should have
<class name="TestClassA">
<methods>
<include name="test_method_1" />
<include name="test_method_2" />
...
</methods>
</class>
<class name="TestClassB">
<methods>
<include name="test_method_1" />
<include name="test_method_2" />
...
</methods>
On using TestNG+Selenium , I'm not able to ensure the order of execution of classes.The order specified below (in testng.xml) is not working ->ClassTwo executes first and then ClassOne is executed.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ABC" parallel="">
<test verbose="2" name="xyz" annotations="JDK" preserve-order="true">
<classes>
<class name="script.ClassOne"/>
<class name="script.ClassTwo"/>
</classes>
</test>
</suite>
How can I ensure that the order specified in TestNG.xml is retained?
You just have to set parallel value to none
<suite name="ABC" parallel="none">
it works for me !
According to TestNG documentation:
By default, TestNG will run your tests in the order they are found in
the XML file. If you want the classes and methods listed in this file
to be run in an unpredictable order, set the preserve-order attribute
to false
I would suggest leaving the preserve-order attribute out, since it is set by default.
However, you have two other options to force specific order to the test methods/classes:
Invoke tests programmatically.
Implement method interceptor, that will order the list of the tests.
Did you try #Test( dependsOnGroups= { "dummyGroupToMakeTestNGTreatThisAsDependentClass" } ) in the class?
See this thread:
TestNG & Selenium: Separate tests into "groups", run ordered inside each group
Hope this helps!
....Quite a bit after the event but I had the same problem and found myself here.
In the end it was because the individual tests had been marked with a priority in the #Test annotation, so in my case but your example script.ClassTwo had a higher priority than script.ClassOne
In TestNG, the order of execution is based on alphabetical order so we could use a TestNG attribute Priority and there we could mention which class->methods you want to execute first.This is Priority annotation attribute you could give in the #Test annotation.
example: #Test(Priority=-1)
Lesser the number value the first it will execute.