java 如何在可执行 jar 中从 main() 运行 TestNG 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16465695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run TestNG tests from main() in an executable jar?
提问by TERACytE
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:
我有一个包含所有依赖项和测试类的可执行 JAR。我已经确认在执行 jar 时调用了 main() 方法。我正在尝试向 main() 添加代码,以便我可以运行特定的 TestNG 测试类。从 TestNG.org 上的文档来看,这似乎是这样做的方法:
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
Is there anyway I can alter my project so that testng.setTestClasses() in main() can access the test class?
无论如何我可以更改我的项目,以便 main() 中的 testng.setTestClasses() 可以访问测试类吗?
回答by TERACytE
I used the following in my main() method and it worked.
我在我的 main() 方法中使用了以下内容并且它起作用了。
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();
回答by KCD
You can load your usual xml in main using org.testng.xml.Parser
and org.testng.xml.XmlSuite
您可以在 main 中使用org.testng.xml.Parser
和加载您常用的 xmlorg.testng.xml.XmlSuite
String xmlFileName = "testng.xml";
List<XmlSuite> suite;
try
{
suite = (List <XmlSuite>)(new Parser(xmlFileName).parse());
testng.setXmlSuites(suite);
testng.run();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
回答by NilsH
If that is your folder structure, and not just a type, it's wrong. The package name is represented as a folder structure, not one folder with the package name.
如果那是您的文件夹结构,而不仅仅是一种类型,那就错了。包名称表示为一种文件夹结构,而不是具有包名称的文件夹。
So it should be src/test/java/com/some/path/tests/MyTests.java
所以应该是 src/test/java/com/some/path/tests/MyTests.java
Also, make sure your test classes are actually in the Jar file. If you're using maven to build the Jar, your test classes will not be included by default.
另外,请确保您的测试类实际上在 Jar 文件中。如果您使用 maven 构建 Jar,则默认情况下不会包含您的测试类。