如何从可执行jar中的main()运行TestNG测试? [英] How to run TestNG tests from main() in an executable jar?

查看:306
本文介绍了如何从可执行jar中的main()运行TestNG测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可执行的JAR,它包含所有依赖项和测试类。我已经确认在执行jar时会调用main()方法。我正在尝试向main()添加代码,以便我可以运行特定的TestNG测试类。从TestNG.org上的文档来看,这似乎就是这样做的:

I have an executable JAR which contains all dependencies and test classes. I've confirmed that the main() method is called when I execute the jar. I'm trying to add code to main() so that I can run a specific TestNG test class. From the documentation on TestNG.org this appears to be the way to do it:

    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] { com.some.path.tests.MyTests.class });
    testng.addListener(tla);
    testng.run();

我的文件夹结构很典型:

My folder structure is typical:

    /src/main/java/Main.java
    /src/test/java/com/some/path/tests/MyTests.java

然而,当我尝试编译它时,我收到此错误:

However when I try to compile it I get this error:

    java: /src/main/java/Main.java:46: package com.some.path.tests does not exist

无论如何,我可以改变我的项目,以便main()中的testng.setTestClasses()可以访问测试类吗?

Is there anyway I can alter my project so that testng.setTestClasses() in main() can access the test class?

推荐答案

我在main()方法中使用了以下内容并且它有效。

I used the following in my main() method and it worked.

    CommandLineOptions options = new CommandLineOptions();
    JCommander jCommander = new JCommander(options, args);

    XmlSuite suite = new XmlSuite();
    suite.setName("MyTestSuite");
    suite.setParameters(options.convertToMap());

    List<XmlClass> classes = new ArrayList<XmlClass>();
    classes.add(new XmlClass("com.some.path.tests.MyTests"));

    XmlTest test = new XmlTest(suite);
    test.setName("MyTests");
    test.setXmlClasses(classes);

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

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

这篇关于如何从可执行jar中的main()运行TestNG测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆