简体   繁体   English

如何在java main方法中添加testng参数?

[英]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.我想从 java main 方法而不是 testng.xml 文件触发执行。

My doubt is how to add the parameters to Java main method for the execution.我的疑问是如何将参数添加到 Java main 方法中以供执行。 I have found .addListener and .setGroups to add listener and groups respectively, but couldn't able to find a way to add parameters.我已经找到 .addListener 和 .setGroups 分别添加侦听器和组,但无法找到添加参数的方法。

Please help me out to start the exection through java main method.请帮助我通过 java main 方法开始执行。

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.如果您重新考虑使用 xml - 您还可以通过带有 xml 文件的 main 方法触发执行。 Add the testng.xml file to your project path (in eclipse you can right click project - new - file - testng.xml), and this will work:将 testng.xml 文件添加到您的项目路径(在 Eclipse 中您可以右键单击项目 - 新建 - 文件 - testng.xml),这将起作用:

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.您也可以通过 arg[0],arg[1] 访问 args。 in cmd run your jar file> java -jar classname.jar param1 param2在 cmd 中运行你的 jar 文件> java -jar classname.jar param1 param2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium-Maven / TestNG:如何在Java类中添加testng参数,同时添加“ main方法”以创建可执行文件/runnable.jar文件? - Selenium - Maven/TestNG: how can we add testng parameters in Java class while adding “main method” to create executable /runnable.jar file? TestNG:如何在命令行上放置Java方法的参数? - TestNG: How i can put parameters for java method on command line? 如何在Java中向方法添加参数 - How to add parameters to a method in java 如何在java的main方法中声明一个带有2个不同类型参数的方法? - How to declare a method with 2 parameters of different types in the main method in java? 如何使TestNG向testng-customsuite.xml添加参数? - How to make TestNG to add parameters to testng-customsuite.xml? 如何在主要的Java方法中添加额外的代码? - How to add extra code into main java method? 从Java main方法调用时如何确定TestNG测试用例的优先级 - How to prioritize TestNG test case when called from java main method 如何在主方法Java调用中处理空参数 - How to handle empty parameters in a main method java call Java调用带有对象参数的方法到main方法 - Java calling a method with object parameters to main method TestNG pass command line arguments to testng.xml from java main method - TestNG pass command line arguments to testng.xml from java main method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM