0

I execute testNG scripts through Jenkins. I have around 75 test classes, but I want to run specific test cases only. To achieve this, I need to pass the test class name to the testNG.XML file. Apparently, this can be achieved through reflection, but I don't know how to proceed with this. Any hint would be appreciated.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62

3 Answers3

0

For achieving the goal of passing test class name to testNG.xml ,consider creating testNG xml programmatically. TestNG objects provides this facility. Refer to the sample code below.

        public static void main(){
         TestNG myTestNG = new TestNG();     
         XmlSuite mySuite = new XmlSuite();
         mySuite.setName("AdviserWebTestSuite");        

         List<XmlTest> myTests = new ArrayList<XmlTest>();

             XmlTest myTest = new XmlTest(mySuite);

             myTest.setName("Created_testsuite_on_runtime");         
             myTest.setXmlClasses(Arrays.asList(new XmlClass("com.example.tests.class1Test")));
             myTest.setXmlClasses(Arrays.asList(new XmlClass("com.example.tests.class2Test")));
             myTest.setXmlClasses(Arrays.asList(new XmlClass("com.example.tests.class3Test")));

             myTests.add(myTest);

         mySuite.setTests(myTests);
         List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
         mySuites.add(mySuite);
         myTestNG.setXmlSuites(mySuites);
         myTestNG.run();
}
Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
0

You don't need reflection for doing this. Assuming you have a TestNG xml file that you already have, then all you have to do is add a BeanShell section to your suite xml file which could inspect the incoming ITestNGMethod object's class name and based on whether there's a match, the method could be included.

Here's a complete working example. For more details please refer to my blog post here

First Class

package com.rationaleemotions.stackoverflow.qn47563557;

import org.testng.annotations.Test;

public class FirstClass {
    @Test
    public void testMethod() {
        System.err.println("FirstClass() says Hello");
    }
}

Second Class

package com.rationaleemotions.stackoverflow.qn47563557;

import org.testng.annotations.Test;

public class SecondClass {
    @Test
    public void testMethod() {
        System.err.println("SecondClass() says Hello");
    }
}

TestNG suite xml file that leverages a beanshell

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="47563557_Suite" verbose="2" parallel="tests" thread-count="10">
    <test name="47563557_Tests">
        <method-selectors>
            <method-selector>
                <script language="beanshell">
                    <![CDATA[
                    whatClass = System.getProperty("classname", "com.rationaleemotions.stackoverflow.qn47563557.FirstClass");
                    currentClass = testngMethod.getTestClass().getName();
                    currentClass.equals(whatClass);
                ]]>
                </script>
            </method-selector>
        </method-selectors>
        <packages>
            <package name="com.rationaleemotions.stackoverflow.qn47563557"/>
        </packages>
    </test>
</suite>

Output

...
... TestNG 6.13.1 by Cédric Beust (cedric@beust.com)
...
FirstClass() says Hello
PASSED: testMethod

===============================================
    47563557_Tests
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
47563557_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Explanation

Here we are making use of a JVM argument classname via which we get the fully qualified class name to be considered so that TestNG may run tests from within it. We have also added a default value so that incase someone doesn't pass anything we still have something to run.

You can enhance the above beanshell to accept csv (comma separate values) for the class names, and then split the csv, iterate through its values to decide which class to run and which not to.

But this should get you started.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
0

I think you can do that with beanshell

I am copy pasting from here

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySuite" parallel="tests" thread-count="10" preserve-order="false">
<parameter name="sauceOs" value="win7" />
    <test name="testName">
         <method-selectors>
             <method-selector>
               <script language="beanshell"><![CDATA[
                 method.getDeclaringClass().getSimpleName().startsWith("ClassNamePrefix")
               ]]></script>
             </method-selector>
        </method-selectors>
        <packages>
            <package name="packageName"></package>
        </packages>
    </test>
</suite>

Also , have a look here

user1207289
  • 3,060
  • 6
  • 30
  • 66