简体   繁体   English

如何从批处理为maven项目运行testng .xml文件

[英]How to run a testng .xml file from batch for a maven project

I have created a maven project and within I have several testng.xml files for my different suites. 我已经创建了一个maven项目,并且我在不同的套件中有几个testng.xml文件。

If I run my .xml files from within my IDE (Intelij) everything goes fine 如果我从我的IDE(Intelij)中运行我的.xml文件,一切都很顺利

But what I want to do is create a batch file that run these .xml for me so I can run it without my IDE. 但我想要做的是为我创建一个运行这些.xml的批处理文件,这样我就可以在没有IDE的情况下运行它。

I've seen a solution when you have a lib folder etc like this : https://www.seleniumeasy.com/testng-tutorials/how-to-run-testng-xml-via-batch-file-example 我有一个像你这样的lib文件夹等的解决方案: https//www.seleniumeasy.com/testng-tutorials/how-to-run-testng-xml-via-batch-file-example

But it doesn't works for me since I have a maven project so no lib or bin folder. 但它不适合我,因为我有一个maven项目,所以没有lib或bin文件夹。

I've also tried this 我也试过这个

SET SUITE_LOCATION=C:\Users\%USERNAME%\IdeaProjects\Test_Automation\src\main\java\MyApp\Scenarios\CustomStuff
SET CLASSPATH=C:\Users\%USERNAME%\.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;
java org.testng.TestNG %SUITE_LOCATION%\testng.xml

or 要么

java -cp org.testng.TestNG %SUITE_LOCATION%\testng.xml

But It doesn't work. 但它不起作用。 I also tried to add the .class files in the classpath but didn't work either. 我还尝试在类路径中添加.class文件,但也没有工作。

Any idea on how to run it ? 有关如何运行它的任何想法?

Thank you! 谢谢!

UPDATE UPDATE

I'm getting close to something I think this is the last command I tried : 我接近了一些事情,我认为这是我试过的最后一个命令:

java -classpath C:\Users\%USERNAME%\.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\%USERNAME%\.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;%CLASSPATH% org.testng.TestNG -d test-outputs mytestsuite.xml

Error was : 错误是:

[TestNG] [ERROR] Cannot find class in classpath: "myApp.Scenarios.Scenario1" [TestNG] [ERROR]在类路径中找不到类:“myApp.Scenarios.Scenario1”

this is my xml file : 这是我的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="AndroidSuite" parallel="tests" thread-count="1">
<test name="SamsungS6-Scenario1">
    <parameter name="deviceUDID"  value="04157df40862d02f"/>
    <classes>
        <class name="myApp.Scenarios.Scenario1"/>
    </classes>
</test>

If you are using maven, run a testsuite like this : 如果您正在使用maven,请运行如下测试套件:

mvn clean test -DsuiteXmlFile=src/test/resources/api/TestngSuite.xml

Add this command to a batch file and execute it. 将此命令添加到批处理文件并执行它。

If you are using maven project and java 8, then you need to do several steps before you run your testng xml file from batch file: 如果您正在使用maven项目和java 8,那么在从批处理文件运行testng xml文件之前,您需要执行几个步骤:

  1. In your pom xml file, you need add a code that will force maven to use 1.8 for the compiler because the default one is 1.5 在你的pom xml文件中,你需要添加一个强制maven为编译器使用1.8的代码,因为默认的是1.5

     <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> 
  2. In your pom xml file, you need to add the code for the maven-surefire-plugin: 在你的pom xml文件中,你需要添加maven-surefire-plugin的代码:

     <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> </suiteXmlFiles> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> 

  3. Save your pom.xml file and right click on your maven project and select maven->update project and check the check box for "force update of snapshots/releases" and click ok 保存您的pom.xml文件并右键单击您的maven项目并选择maven-> update project并选中“强制更新快照/版本”复选框,然后单击“确定”。

  4. Now create a batch file with an extension bat and put this sample code in it and change it according to your project. 现在创建一个带有扩展bat的批处理文件,并将此示例代码放入其中,并根据您的项目进行更改。

    set projectLocation=C:\\locationOfYourProject\\locationOfYourTestNGXmlFile set projectLocation = C:\\ locationOfYourProject \\ locationOfYourTestNGXmlFile

    cd %projectLocation% cd%projectLocation%

    mvn clean test -DsuiteXmlFile=yourXMLFileCanBeAnyName.xml mvn clean test -DsuiteXmlFile = yourXMLFileCanBeAnyName.xml

Even after including the jcommander and the testng jar in the class path while executing we would be required to reach to a folder from where the main package of the application can be reached 即使在执行过程中将jcommander和testng jar包含在类路径中,我们也需要到达可以访问应用程序主包的文件夹

  1. For example if the *Test.java files are present in com.test.app package then the folder structure for the same with respect to the Project will be src/test/java/com/test/app 例如,如果* test.java文件存在于com.test.app包中,那么相对于Project的文件夹结构将是src / test / java / com / test / app

so on executing 所以执行

javac -cp ".:/home/manish/.m2/repository/org/testng/testng/6.9.8/testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar" $(find -name *.java) javac -cp“。:/ home / manish / .m2 / repository / org / testng / testng / 6.9.8 / testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/ 1.48 / jcommander-1.48.jar“$(find -name * .java)

will compile the java files in their respective package 将在各自的包中编译java文件

user will have to go to the test/java folder (if user is in project root folder) 用户必须转到test / java文件夹(如果用户在项目根文件夹中)

cd src/test/java
javac -cp ".:/home/manish/.m2/repository/org/testng/testng/6.9.8/testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar" org.testng.TestNg testng.xml_file_path_relative_to_current_location
  1. If the java files are compiled using the mvn compiler:testCompile 如果使用mvn编译器编译java文件:testCompile

then the user will have to go to the target/test-classes folder 然后用户必须转到target / test-classes文件夹

cd target/test-classes 
javac -cp ".:/home/manish/.m2/repository/org/testng/testng/6.9.8/testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar" org.testng.TestNg testng.xml_file_path_relative_to_current_location

both the above ways are executing the tests 以上两种方式都在执行测试

java -classpath C:\\Users\\%USERNAME%.m2\\repository\\org\\testng\\testng\\6.9.10\\testng-6.9.10.jar;C:\\Users\\%USERNAME%.m2\\repository\\com\\beust\\jcommander\\1.48\\jcommander-1.48.jar;%CLASSPATH% org.testng.TestNG -d test-outputs mytestsuite.xml java -classpath C:\\ Users \\%USERNAME%。m2 \\ repository \\ org \\ testng \\ testng \\ 6.9.10 \\ testng-6.9.10.jar; C:\\ Users \\%USERNAME%。m2 \\ repository \\ com \\ beust \\ jcommander \\ 1.48 \\ jcommander-1.48.jar;%CLASSPATH%org.testng.TestNG -d test-outputs mytestsuite.xml

In this, your jar file is missing and hence the class not found exception. 在这种情况下,您的jar文件丢失,因此找不到类的异常。

java -classpath C:\\Users\\%USERNAME%.m2\\repository\\org\\testng\\testng\\6.9.10\\testng-6.9.10.jar;C:\\Users\\%USERNAME%.m2\\repository\\com\\beust\\jcommander\\1.48\\jcommander-1.48.jar;%CLASSPATH%; java -classpath C:\\ Users \\%USERNAME%。m2 \\ repository \\ org \\ testng \\ testng \\ 6.9.10 \\ testng-6.9.10.jar; C:\\ Users \\%USERNAME%。m2 \\ repository \\ com \\ beust \\ jcommander \\ 1.48 \\ jcommander-1.48.jar;%CLASSPATH%; org.testng.TestNG -d test-outputs mytestsuite.xml org.testng.TestNG -d test-outputs mytestsuite.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM