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

查看:49
本文介绍了如何在可执行 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天全站免登陆