Java 如何为Testng创建一个可执行的jar文件,运行点应该是Xml文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16393223/
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 create a executable jar file for Testng and the runnnig point should be the Xml file
提问by mannu singh
I am currently working on the selenium web driver and testng on Eclipse IDE. I usually run the test from the XML file that i have created which runs all the methods in the eclipse.
我目前正在 Eclipse IDE 上开发 selenium Web 驱动程序和 testng。我通常从我创建的 XML 文件运行测试,该文件运行 eclipse 中的所有方法。
Now i want to create a simple executable jar which should do the same i.e its running point should be the XML file so that each test is executed .
现在我想创建一个简单的可执行 jar,它应该做同样的事情,即它的运行点应该是 XML 文件,以便执行每个测试。
I am trying hard on this. Please give me some advice on how to go further with it
我正在为此努力。请给我一些关于如何更进一步的建议
采纳答案by sanbhat
Use Eclipse Export Wizard. While exporting, select "Create Runnable Jar" and select the class which is entry point(which contains main
method) of your project.
使用Eclipse 导出向导。导出时,选择“Create Runnable Jar”并选择作为项目入口点(包含main
方法)的类。
This class will have main
method which will read XML and execute the testcases
此类将具有main
读取 XML 并执行测试用例的方法
回答by mannu singh
Here is the better way to do it. But thanks anyways sanbhat.
这是更好的方法。但无论如何感谢sanbhat。
You can just create a main method which will have list of all test classes to be executed as follows:
您可以创建一个 main 方法,该方法将包含要执行的所有测试类的列表,如下所示:
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { test_start.class });
testng.addListener(tla);
testng.run();
}
Here is the reference URL from the official testng website.
这是来自官方 testng 网站的参考 URL。
http://testng.org/doc/documentation-main.html#running-testng-programmatically
http://testng.org/doc/documentation-main.html#running-testng-programmatically
Cheers!
干杯!
回答by user2801388
Creating a jar File in Command Prompt
在命令提示符中创建 jar 文件
Start Command Prompt.
Navigate to the folder that holds your class files:
启动命令提示符。
导航到保存类文件的文件夹:
C:\>cd \lalit
Set path to include JDK's bin. For example:
设置路径以包含 JDK 的 bin。例如:
C:\lalit> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%
Compile your class(es):
编译你的类:
C:\lalit> javac *.java
Create a manifest file and your jar file:
创建一个清单文件和你的 jar 文件:
C:\lalit> echo Main-Class: hitech >manifest.txt
C:\lalit> jar cvfm hitech.jar manifest.txt *.class
or
或者
C:\lalit> jar cvfe hitech.jar hitech *.class
Test your jar:
测试你的罐子:
C:\lalit> hitech.jar
or
或者
C:\lalit> java -jar hitech.jar
回答by Ali786
You can create a main method like below and can execute it
您可以创建一个如下所示的主要方法并可以执行它
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("c:/tests/testng1.xml");//path to xml..
suites.add("c:/tests/testng2.xml");
testng.setTestSuites(suites);
testng.run();
}