简体   繁体   中英

How to add testng parameters in a java main method?

I wanted to trigger the execution from a java main method instead of testng.xml file.

My doubt is how to add the parameters to Java main method for the execution. I have found .addListener and .setGroups to add listener and groups respectively, but couldn't able to find a way to add parameters.

Please help me out to start the exection through java main method.

Sample:

public class Execution {
    public static void main(String[] args) throws IOException {
        TestNG test = new TestNG();
        test.setTestClasses(new Class[] {AETVTests.class});
        test.addListener(new MyTestListenerAdapter());
        test.setGroups("");
        test.run();
    }
}

If you would reconsider using xml - you can also trigger execution through the main method with the xml file. Add the testng.xml file to your project path (in eclipse you can right click project - new - file - testng.xml), and this will work:

public static void main(String[] args) throws IOException
{
    TestNG testng = new TestNG();

    List<String> suites = Lists.newArrayList();
    suites.add("C:\\eclipse-2018\\Tests\\testng.xml");  //path to xml
    testng.setTestSuites(suites);

    testng.run(); //run TestNG   
}

You can access args by arg[0],arg[1] likewise. in cmd run your jar file> java -jar classname.jar param1 param2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM