首页 > 解决方案 > 如何为放心项目创建 .bat 文件?

问题描述

我正在开发一个包含 testng.xml 文件的 Rest-Assured 项目。我正在尝试通过批处理运行 testng.xml 文件。但这给了我错误-

无法找到或加载主类 org.testng.TestNG

以下是批处理文件的代码:-

cd C:\Users\workspace\RestAssured
set projectPath=C:\Users\workspace\RestAssured
java org.testng.TestNG "%projectPath%\testng.xml"
pause

我经历了许多批处理文件创建解决方案,但我仍然无法复制它。而且,在某些解决方案中,我看到他们还添加了类路径,例如 - set classpath=C:\Users\workspace\RestAssured\bin。但是这个 bin 文件夹在我的项目中不存在。

而且,假设我的项目位于 C 盘位置。如何为不同的用户动态取项目路径?

标签: batch-filetestngrest-assured

解决方案


You need to ensure that you set the class path properly.

Here's what you need to do:

  1. Download the latest version of TestNG 7.0.0 from here.
  2. Download the dependencies that TestNG and have them made available in a folder (say lib). The easiest way of figuring out the actual download URLs is by doing http://repo1.maven.org/maven2" + groupId + "/ + artifactId + "/" + version (replace all . with /)
<dependency>
    <groupId>com.beust</groupId>
    <artifactId>jcommander</artifactId>
    <version>1.72</version>
</dependency>
<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant</artifactId>
    <version>1.10.3</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>1.21</version>
</dependency>
  1. Once you have everything in your lib folder, create a batch file wherein you set the classpath to refer to all the jars in the lib folder,

    cd C:\Users\workspace\RestAssured set projectPath=%cd% java -classpath "lib/*" org.testng.TestNG "%projectPath%\testng.xml" pause

This should take care of all of your questions.


推荐阅读