Customizing TestNG.xml to pass run time tests

216 views
Skip to first unread message

Venkata Anil Babu G

unread,
Aug 19, 2019, 5:27:16 AM8/19/19
to seleniu...@googlegroups.com
Hi All,
We are trying to run the test scripts from Jenkins.Sometimes our requirements is we wanted to run specific tests from Jenkins.
I tried creating a listener from https://rationaleemotions.com but the listener is not creating run time testng.xml or beanshell which will create specific class to execute testng.xml

package UtilClasses.UI;

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SimpleSuiteAlterer implements IAlterSuiteListener {
@Override
    public void alter(List<XmlSuite> suites)
    {
        /*User is passing the test tag names here that he/she wish to execute.
         *This is going to be a Multi select parameter from Jenkin .. active choice parameter allows multi select
         *
         */
       
        String testsToExecute = System.getProperty("TestScripts","");
        // Alter the suite object only if testNames passed is not empty
        if(!testsToExecute.equals(""))
        {
            String[] testsPassedByUser = testsToExecute.split(",");
            XmlSuite suite = suites.get(0);
            List<XmlTest> xmlTests = suite.getTests();

            ArrayList<XmlTest> newXMLTests = new ArrayList<XmlTest>();

            for (String testName : testsPassedByUser)
            {                                
                for (XmlTest xmlTest : xmlTests)
                    {
                        String name = xmlTest.getName();
                        if(name.equalsIgnoreCase(testName))
                        {
                            newXMLTests.add(xmlTest);
                        }
                    }    
            }
            // suite object is altered here with the new set of xml tests
            suite.setTests(newXMLTests);

    }

}
}

⇜Krishnan Mahadevan⇝

unread,
Aug 19, 2019, 1:15:57 PM8/19/19
to Selenium Users
The blog being quoted is mine.

>>>> I tried creating a listener from https://rationaleemotions.com but the listener is not creating run time testng.xml or beanshell which will create specific class to execute testng.xml

Not sure what you are expecting from an IAlterSuiteListener implementation.


Here's a working example that demonstrates how to do this

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.testng.IAlterSuiteListener;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class ExampleTest {

@Test
public void test() {
System.err.println("test() ==> " + Reporter.getCurrentTestResult().getTestContext().getName());
}

public static class ExampleSuiteAlterer implements IAlterSuiteListener {


@Override
public void alter(List<XmlSuite> suites) {
      String tests = System.getProperty("tests", "t1"); //only <t1> should be executed
if (!tests.trim().isEmpty()) {
List<String> testNamesToInclude = Arrays.asList(tests.split(","));
List<XmlTest> filtered = suites.get(0).getTests().stream()
.filter(xmlTest -> testNamesToInclude.contains(xmlTest.getName()))
.collect(Collectors.toList());
suites.get(0).getTests().clear();
suites.get(0).getTests().addAll(filtered);
}
}
}
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Implementing Parametrization">
<listeners>
<listener class-name="com.rationaleemotions.ExampleTest$ExampleSuiteAlterer"/>
</listeners>
<test name="t1">
<classes>
<class name="com.rationaleemotions.ExampleTest"/>
</classes>
</test>
<test name="t2">
<classes>
<class name="com.rationaleemotions.ExampleTest"/>
</classes>
</test>
</suite>
test() ==> t1

===============================================
Implementing Parametrization
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 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 Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CACvSpted9YBNRg3cNkybmwjSqyfLfvXUCMpCZeX_RWnTNaZC5w%40mail.gmail.com.

Venkata Anil Babu G

unread,
Aug 20, 2019, 12:42:19 AM8/20/19
to seleniu...@googlegroups.com
I very much big follower of your blog as its big learning for so many of us.Small clarification will the testng.xml be default created i just kept listeners in testng.xml without any test tag but i dont see anything executed of specific class what i selected in jenkins.xml

Krishnan Mahadevan

unread,
Aug 20, 2019, 12:44:31 AM8/20/19
to seleniu...@googlegroups.com
Can you pleas create a sample project which I can use to recreate the problem and share it with us ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Venkata Anil Babu G

unread,
Aug 20, 2019, 1:12:25 AM8/20/19
to seleniu...@googlegroups.com
Hi
Iam attaching a sample project.I  want to select tests to be selected from jenkins so i have created a parameter-Testscripts  with values-REST.tests.IndexCreation,REST.tests.Sample02.When a user selects specific script IndexCreation then my jenkins should pass that parameter to testng and testng should execute the specific tests or if i select all tests then it should run all the tests.


Krishnan Mahadevan

unread,
Aug 20, 2019, 11:31:42 PM8/20/19
to seleniu...@googlegroups.com

Anil,

 

Please clean up your project and upload something into github that can be used to reproduce the problem. You seem to have just uploaded your current project into google drive.

 

The project you shared right now doesn’t have any suite xml file, nor a listener and I am not sure what test to be executing.

 

The sample project needs to have only the following:

 

  1. It would be a maven or a gradle project
  2. It would have two or more test classes which has one or more “@Test” methods, which does just a println of the method names.
  3. It would have your IAlterSuiteListener implementation, that you are trying to use and which you say doesn’t work for you.
  4. Its surefire plugin configuration would match with what you are using in real-time.

 

PS:

Please help put it on github instead of google drive

 

 

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.com/

Reply all
Reply to author
Forward
0 new messages