简体   繁体   English

如何在没有 testng.xml 文件的情况下执行 Testng 和 Maven

[英]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?如何配置 pom.xml 文件,以便我可以在不使用 testng.xml 文件的情况下运行我的测试脚本? As you know that we can dynamically create testng.xml file using XmlSuite, XmlClass, XmlTest and TestNg object.如您所知,我们可以使用 XmlSuite、XmlClass、XmlTest 和 TestNg 对象动态创建 testng.xml 文件。

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();



}

} }

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. Maven 将在mvn test生命周期阶段触发任何测试,前提是您遵循了Maven 标准目录布局和任何测试框架或插件要求。

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 .对于 TestNG,您可以查看将Surefire 插件与 TestNG 一起使用,如果您遵循surefire 的测试命名约定,它将执行您的测试

Additionally, here's a fairly recent article that goes a bit more in depth with a TestNG setup example .此外,这是一篇最近发表的文章,其中更深入地介绍了TestNG 设置示例

我不知道这是否是可以接受的答案,但如果您使用的是 Eclipse,则可以安装 TestNG 插件,然后从 Eclipse 触发脚本,它会自动创建必要的 XML 文件。

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

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