Running tests in a package programmatically

349 views
Skip to first unread message

Raghuram Devarakonda

unread,
Dec 10, 2012, 10:59:47 AM12/10/12
to testng...@googlegroups.com
Hi,

I am having trouble in programmatically running tests contained in a package. If I add the test class directly, TestNG runs the tests fine but if I add the package and not the class, the tests are not discovered. Here is a code snippet:

-----
        XmlPackage pack = new XmlPackage("ExamplePackage");
        pack.setInclude(Arrays.asList("org.explore.config"));

        XmlSuite suite = new XmlSuite();
        suite.setName("ExampleSuite");

        XmlTest test = new XmlTest(suite);
        test.setName("ExampleTest");
        test.setPackages(Arrays.asList(pack));
        // XmlClass testClass = new XmlClass(ConfigTest.class);
        // test.setClasses(Arrays.asList(testClass));

        suite.setTests(Arrays.asList(test));

        TestNG testng = new TestNG();
        testng.setXmlSuites(Arrays.asList(suite));
        testng.run();
-----

Am I missing something here? I am attaching a small maven project that demonstrates the problem. 

Thanks,
Raghu
testngsuite.tar.bz2

Krishnan Mahadevan

unread,
Feb 4, 2013, 5:17:26 AM2/4/13
to testng...@googlegroups.com
Raghu,
Here's how you do it :


package com.test;

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

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

public class RunningTestsInPackage {

    /**
     * @param args
     */
    public static void main(String[] args) {
        TestNG testng = new TestNG();
        testng.setXmlSuites(getSuite());
        testng.run();

    }

    public static List<XmlSuite> getSuite() {
        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        XmlSuite eachSuite = new XmlSuite();
        eachSuite.setName("My Suite");
        eachSuite.setTests(getTest(eachSuite));
        System.out.println(eachSuite.toXml());
        suites.add(eachSuite);
        return suites;
    }

    public static List<XmlTest> getTest(XmlSuite suite) {
        List<XmlTest> tests = new ArrayList<XmlTest>();
        XmlTest eachTest = new XmlTest();
        tests.add(eachTest);
        eachTest.setName("My test");
        eachTest.setPackages(getPackages());
        eachTest.setSuite(suite);
        return tests;
    }

    public static List<XmlPackage> getPackages() {
        List<XmlPackage> allPackages = new ArrayList<XmlPackage>();
        XmlPackage eachPackage = new XmlPackage();
        eachPackage.setName("com.test.dummy");
        allPackages.add(eachPackage);
        return allPackages;
    }
}

Output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Suite">
  <test name="My test" preserve-order="true">
    <packages>
      <package name="com.test.dummy"/>
    </packages>
  </test> <!-- My test -->
</suite> <!-- My Suite -->

[TestNG] Running:
  Command line suite

com.test.dummy.One.foo()
com.test.dummy.Two.bar()

===============================================
My Suite
Total tests run: 2, Failures: 0, Skips: 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/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Kiran

unread,
Jan 23, 2016, 3:53:20 PM1/23/16
to testng-users
Hi Krishnan,

Above code is generating xml file correctly but not running any tests.It runs my tests when I save and run the printed xml file 

Krishnan Mahadevan

unread,
Jan 23, 2016, 9:59:22 PM1/23/16
to testng...@googlegroups.com
I can still execute this demo code Kiran. You might have to check what is wrong at your end.

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

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages