How to run selective tests from testng.xml - automation

I have testng.xml in which I have large number of <test> tag. I want to execute some of them not all and I don't want to comment the rest
e.g In my testng.xml, there are three tests
<test name="testA">
</test>
<test name="testB">
</test>
<test name="testC">
</test>
I want to execute testA and testC only and i don't want to comment the testB
Any suggestion..?

Add a command line switch of: -testnames=testA,testC

I don't think that can be done. If there are specific tests you do not want to run then you can check out Iannotationtransformer to set the enabled value = false to set specific tests not to run.

Related

Running specific test class inside a package first in TestNG

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.

"Executing a test in testNG based on condition"

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/

TestNG test are not getting executed as per the priority

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.

Jenkins TestNG result integration

I have lots of longrunning separate tests, grouped by functionality in suites in some xml files.
<suite name="Suite1" thread-count="1" parallel="false" verbose="1">
<test name="A" preserve-order="true">
<classes>
...
</classes>
</test>
<test name="B" preserve-order="true">
<classes>
...
</classes>
</test>
</suite>
My problem is, I'd like to generate some kind of html/xml result from grouping the results based on their test name / tag.
For example, I have these tests from A to E, and the result groups woudl be :
A,B,C
B,D,E
I know I could create a separate suite for the tasks (A,B,C) but in that case B would execute again in the second suite, which is not desirable since they are long running... So the idea would be to run every test once, but generate combined reports based on their name/tag.
Is there a plugin for this ? Or how could I achieve this ?
You can make your own reporter which will be generated the expected result.
The default reporter could be a good start.

how to run test cases parallely using selenium + testng

I wanted to run test cases parallely with different parameters . My testng.xml would look like this
<suite name="Automation Testing" preserve-order="true">
<parameter name="browser" value="#BROWSER#"/>
<test name="Login">
<classes>
<class name="common.Login" > </class>
</classes>
</test>
</suite>
Java Class which accept parameters
#parameters({'browser'})
#beforesuite
public void login(String browser){
if(broswer.equals('ff')){
WebDriver driver = new FirefoxDriver()
}
}
I have gone through this link https://stackoverflow.com/questions/14625256/how-to-run-the-test-in-parallel-using-selenium-webdrivertestng ... But i have not anything out it ... please let me know how i can do it .....
If you want the same testcase to be run on different browsers, there are different ways.
Define multiple <test> in our xml file and have individual parameters. You would need to have a new name for each #BROWSER# i.e. #BROWSER1, #BROWSER2. Then run with parallel = tests which would trigger the same testcases on different browsers. However, the issue with this approach is you will have to always supply fixed number of browses to your ant xml
Another approach can be, to supply a comma separated list to your #BROWSER# parameter. Use a dataprovider to do a split and create driver instances. Set parallel= methods.
Set parallel= methods. Control browser based paralle runs from outside i.e Trigger various builds if you are using something like Jenkins with different parameter values for #BROWSER#