user1459497
user1459497

Reputation: 657

How to execute Testng and Maven without testng.xml file

How to configure pom.xml file so that i can run my test scripts without using testng.xml file? As you know that we can dynamically create testng.xml file using XmlSuite, XmlClass, XmlTest and TestNg object.

public class ScriptTest010 {

public static void main(String[] args) {

    List<XmlSuite> suites = new ArrayList<XmlSuite>();
    XmlSuite suite = new XmlSuite();
    suites.add(suite);


    List<XmlClass> classes = new ArrayList<XmlClass>();
    XmlClass clz = new XmlClass();
    clz.setClass(SampleTest.class);
    classes.add(clz);


    XmlTest test = new XmlTest(suite);
    test.setClasses(classes);

    Map<String, String> params = new HashMap<String, String>();
    params.put("username", "mquraishi");
    params.put("password", "nopassword1$");
    params.put("search", "eat pray love");


    test.setParameters(params);


    XmlInclude testLogin = new XmlInclude("testLogin");

    List<XmlInclude> includes = new ArrayList<>();
    includes.add(testLogin);

    clz.setIncludedMethods(includes);


    TestNG testNg = new TestNG();
    testNg.setXmlSuites(suites);
    testNg.run();



}

}

Upvotes: 5

Views: 8540

Answers (2)

JeffC
JeffC

Reputation: 25542

I don't know if this is an acceptable answer but if you are using Eclipse, there is a plugin for TestNG that you can install and then trigger the scripts from Eclipse and it will auto-create the necessary XML files.

Upvotes: 1

whitlaaa
whitlaaa

Reputation: 922

Maven will trigger any tests during the mvn test lifecycle phase provided you have followed the Maven Standard Directory Layout and any test framework or plugin requirements.

For TestNG, you can take a look at using the Surefire plugin with TestNG which will execute your tests if you follow surefire's test naming conventions.

Additionally, here's a fairly recent article that goes a bit more in depth with a TestNG setup example.

Upvotes: 4

Related Questions