简体   繁体   English

如何从命令行运行 TestNG

[英]How to run TestNG from command line

How exactly do I run a.java TestNG project from a command line?我究竟如何从命令行运行 a.java TestNG 项目?

I have read through the TestNG documentation, and tried the following to no avail:我已通读 TestNG 文档,并尝试以下方法无济于事:

C:\projectfred> java org.testng.TestNG testng.xml 

... with the following testng.xml file in my project: ...在我的项目中使用以下 testng.xml 文件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  
<suite name="SuiteAll" verbose="1">
    <test name="TestAll">  
        <packages>  
            <package name="com.project.fred.tests"/>
        </packages>  
    </test>  
</suite>

The error I get is this:我得到的错误是这样的:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.testng.TestNG.  Program will exit.

Obviously, I am not referencing TestNG correctly in my command line.显然,我没有在命令行中正确引用 TestNG。 Does anyone know how to get this working?有谁知道如何让这个工作?

You need to have the testng.jar under classpath.你需要在类路径下有testng.jar

try C:\\projectfred> java -cp "path-tojar/testng.jar:path_to_yourtest_classes" org.testng.TestNG testng.xml试试C:\\projectfred> java -cp "path-tojar/testng.jar:path_to_yourtest_classes" org.testng.TestNG testng.xml

Update:更新:

Under linux I ran this command and it would be some thing similar on Windows either在 linux 下我运行了这个命令,它在 Windows 上也有类似的东西

test/bin# java -cp ".:../lib/*" org.testng.TestNG testng.xml

Directory structure:目录结构:

/bin - All my test packages are under bin including testng.xml
/src - All source files are under src
/lib - All libraries required for the execution of tests are under this.

Once I compile all sources they go under bin directory.一旦我编译了所有源,它们就会在 bin 目录下。 So, in the classpath I need to specify contents of bin directory and all the libraries like testng.xml, loggers etc over here.因此,在类路径中,我需要在此处指定 bin 目录和所有库(如 testng.xml、loggers 等)的内容。 Also copy testng.xml to bin folder if you dont want to specify the full path where the testng.xml is available.如果您不想指定 testng.xml 可用的完整路径,也将 testng.xml 复制到 bin 文件夹。

 /bin
    -- testng.xml
    -- testclasses
    -- Properties files if any.
 /lib
    -- testng.jar
    -- log4j.jar

Update : Update

Go to the folder MyProject and type run the java command like the way shown below:-转到文件夹MyProject并键入运行 java 命令,如下所示:-

java -cp ".: C:\Program Files\jbdevstudio4\studio\plugins\*" org.testng.TestNG testng.xml

I believe the testng.xml file is under C:\\Users\\me\\workspace\\MyProject if not please give the full path for testng.xml file我相信 testng.xml 文件在C:\\Users\\me\\workspace\\MyProject如果不是,请提供testng.xml文件的完整路径

If you are using Maven, you can run it from the cmd line really easy, cd into the directory with the testng.xml (or whatever yours is called, the xml that has all the classes that will run) and run this cmd:如果您使用的是 Maven,您可以非常轻松地从 cmd 行运行它,cd 到带有 testng.xml 的目录(或任何名称,具有将运行的所有类的 xml)并运行此 cmd:

mvn clean test -DsuiteXmlFile=testng.xml 

This page explains it in much more detail: How to run testng.xml from Maven command line此页面更详细地解释了它: How to run testng.xml from Maven command line

I didn't know it mattered if you were using Maven or not so I didn't include it in my search terms, I thought I would mention it here in case others are in the same situation as I was.我不知道您是否使用 Maven 是否重要,所以我没有将它包含在我的搜索词中,我想我会在这里提到它,以防其他人与我处于相同的情况。

Ok after 2 days of trying to figure out why I couldn't run the example from好的,经过 2 天的尝试弄清楚为什么我无法从以下位置运行示例

http://www.tutorialspoint.com/testng/testng_environment.htm the following code did not work for me http://www.tutorialspoint.com/testng/testng_environment.htm以下代码对我不起作用

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

The fix for it is as follows: I came to the following conclusion: First off install eclipse, and download the TestNG plugin.修复方法如下: 我得出以下结论:首先安装eclipse,然后下载TestNG插件。 After that follow the instructions from the tutorial to create and compile the test class from cmd using javac, and add the testng.xml.之后,按照教程中的说明使用 javac 从 cmd 创建和编译测试类,并添加 testng.xml。 To run the testng.xml on windows 10 cmd run the following line:要在 Windows 10 cmd 上运行 testng.xml,请运行以下行:

java -cp C:\Users\Lenovo\Desktop\eclipse\plugins\org.testng.eclipse_6.9.12.201607091356\lib\*;C:\Test org.testng.TestNG testng.xml

to clarify: C:\\Users\\Lenovo\\Desktop\\eclipse\\plugins\\org.testng.eclipse_6.9.12.201607091356\\lib\\* The path above represents the location of jcommander.jar and testng.jar that you downloaded by installing the TESTNG plugin for eclipse.澄清一下: C:\\Users\\Lenovo\\Desktop\\eclipse\\plugins\\org.testng.eclipse_6.9.12.201607091356\\lib\\*上面的路径代表你安装TESTNG下载的jcommander.jar和testng.jar的位置eclipse插件。 The path may vary so to be sure just open the installation location of eclipse, go to plugins and search for jcommander.jar.路径可能会有所不同,因此请确保只需打开 eclipse 的安装位置,转到 plugins 并搜索 jcommander.jar。 Then copy that location and after that add * to select all the necessary .jars.然后复制该位置,然后添加 * 以选择所有必需的 .jar。

C:\Test  

The path above represents the location of the testing.xml in your project.上面的路径表示 testing.xml 在您的项目中的位置。 After getting all the necessary paths, append them by using ";".获得所有必要的路径后,使用“;”附加它们。

I hope I have been helpful to some of you guys :)我希望我对你们中的一些人有所帮助:)

Thanks a lot it was really very helpful.. I was able to run my TestNG classes using command line, but still I would like to wrap-up the things one last time as follows..非常感谢,它真的很有帮助..我能够使用命令行运行我的 TestNG 类,但我仍然想最后一次总结这些事情如下..

1) Set the classpath for the testng.jar in the environment variables
(either manually or via command line).

2) Create a new folder "MyTest".

3) Compile all your classes(testNG+java) and put them(.class files )
in a new folder "Mytest\bin".

4) Put your 'testng.xml' and '.properties' files to "Mytest\bin"
again.

5) Put all the .jar files (testng,selenium... etc) in a new folder
"Mytest\lib".

6) Browse to the folder "bin" via command line and run the command
java -cp "..\lib\*;" org.testng.TestNG testng.xml

this should run your testNG cases.这应该运行您的 testNG 案例。

Using " ; " as delimiter on windows issue got resolved.在 Windows 问题上使用“;”作为分隔符得到了解决。

java -cp "path of class files; testng jar file path" org.testng.TestNG testng.xml

ex:-前任:-

java -cp ".\bin;..\common_lib\*;" org.testng.TestNG testng.xml

Prepare MANIFEST.MF file with the following content准备MANIFEST.MF文件,内容如下

Manifest-Version: 1.0
Main-Class: org.testng.TestNG

Pack all test and dependency classes in the same jar, say tests.jar将所有测试和依赖类打包在同一个 jar 中,比如tests.jar

jar cmf MANIFEST.MF tests.jar -C folder-with-classes/ .

Notice trailing ".", replace folder-with-classes/ with proper folder name or path.注意尾随的“.”,用正确的文件夹名称或路径替换folder-with-classes/

Create testng.xml with content like below使用如下内容创建testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Tests" verbose="5">
  <test name="Test1">
    <classes>
      <class name="com.example.yourcompany.qa.Test1"/>
    </classes>
  </test>  
</suite>

Replace com.example.yourcompany.qa.Test1 with path to your Test class.com.example.yourcompany.qa.Test1替换为 Test 类的路径。

Run your tests运行你的测试

java -jar tests.jar testng.xml

I had faced same problem because I downloaded TestNG plugin from Eclipse.我遇到了同样的问题,因为我从 Eclipse 下载了 TestNG 插件。 Here what I did to get the Job done :这是我为完成工作所做的工作:

  1. After adding TestNG to your project library create one folder in your Project names as lib ( name can be anything ) :将 TestNG 添加到您的项目库后,在您的项目名称中创建一个文件夹作为 lib(名称可以是任何内容):

  2. Go to "C:\\Program Files\\Eclipse\\eclipse-java-mars-R-win32-x86_64\\eclipse\\plugins" location and copy com.beust.jcommander_1.72.0.jar and org.testng_6.14.2.r20180216145.jar file to created folder (lib).转到“C:\\Program Files\\Eclipse\\eclipse-java-mars-R-win32-x86_64\\eclipse\\plugins”位置并复制com.beust.jcommander_1.72.0.jarorg.testng_6.14.2.r20180216145.jar文件到创建的文件夹(lib)。

Note : Files are testng.jar and jcommander.jar注意:文件是 testng.jar 和 jcommander.jar

  1. Now Launch CMD, and navigate to your project directory and then type :现在启动 CMD,并导航到您的项目目录,然后键入:
    Java -cp C:\\Users\\User123\\TestNG\\lib*;C:\\Users\\User123\\TestNG\\bin org.testng.TestNG testng.xml Java -cp C:\\Users\\User123\\TestNG\\lib*;C:\\Users\\User123\\TestNG\\bin org.testng.TestNG testng.xml

That's it !就是这样 !
Let me know if you have any more concerns.如果您还有其他问题,请告诉我。

Below command works for me.下面的命令对我有用。 Provided that all required jars including testng jar kept inside lib.前提是所有必需的 jars 包括 testng jar 都保存在 lib 中。

java -cp "lib/*" org.testng.TestNG testng.xml

You will need to use semicolon as delimiter while specifying the jar and class file path in windows.在 Windows 中指定 jar 和类文件路径时,您需要使用分号作为分隔符。 This solved the issue.这解决了这个问题。
Assuming the class file is under C:.假设类文件在C:

java -cp ".;C:\testng.jar" org.testng.TestNG testing.xml

After gone throug the various post, this worked fine for me doing on IntelliJ Idea :在浏览了各种帖子之后,这对我在IntelliJ Idea上做的很好:

java -cp "./lib/*;Path to your test.class"  org.testng.TestNG testng.xml

Here is my directory structure:这是我的目录结构:

/lib
  -- all jar including testng.jar
/out
  --/production/Example1/test.class
/src
 -- test.java
testing.xml

So execute by this command:所以通过这个命令执行:

java -cp "./lib/*;C:\Users\xyz\IdeaProjects\Example1\out\production\Example1" org.testng.TestNG testng.xml

My project directory Example1 is in the path:我的项目目录Example1在路径中:

C:\Users\xyz\IdeaProjects\

If none of the above answers work, you can run the test in IDE, get the class path and use it in your command.如果以上答案都不起作用,您可以在 IDE 中运行测试,获取类路径并在您的命令中使用它。 Ex: If you are using Intellij IDEA, you can find it at the top of the console(screenshot below).例如:如果您使用的是 Intellij IDEA,您可以在控制台顶部找到它(下面的屏幕截图)。 Intellij 控制台

Clicking on the highlighted part expands and displays the complete class path.单击突出显示的部分将展开并显示完整的类路径。

you need to remove the references to jars inside the folder: JetBrains\\IntelliJ IDEA Community Edition 2019.1.3您需要删除文件夹中对 jars 的引用:JetBrains\\IntelliJ IDEA Community Edition 2019.1.3

java -cp "path_copied" org.testng.TestNG testng.xml

I tried everything on this post but it turns out I was missing a jar file.我尝试了这篇文章中的所有内容,但结果发现我丢失了一个 jar 文件。 Here's my solution:这是我的解决方案:

Add guice-4.2.2.jar to your jar files if you are getting the following error.如果您收到以下错误,请将 guice-4.2.2.jar 添加到您的 jar 文件中。 If you are using maven then add the following dependency.如果您使用的是 maven,则添加以下依赖项。

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.1.0</version>
</dependency>


Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Stage
        at org.testng.internal.Configuration.<init>(Configuration.java:33)
        at org.testng.TestNG.init(TestNG.java:216)
        at org.testng.TestNG.<init>(TestNG.java:200)
        at org.testng.TestNG.privateMain(TestNG.java:1312)
        at org.testng.TestNG.main(TestNG.java:1304)
Caused by: java.lang.ClassNotFoundException: com.google.inject.Stage
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 5 more

Try below command试试下面的命令

java -cp bin\;jars\* org.testng.TestNG testng.xml

where bin has the compiled classes of your projects and jars folder contains the necessary jars to execute your testng.xml file其中bin包含项目的编译类, jars文件夹包含执行 testng.xml 文件所需的 jars

Make sure not to use bin\* instead of bin\ as it will lead you to errors.

Now necessary jars will be your TestNG jar, jcommander jar, and the bsh jar, you can download these jars from here现在必需的 jars 将是您的 TestNG jar、jcommander jar 和 bsh jar,您可以从这里下载这些 jars

Your project structure would be something like below -您的项目结构如下所示 -

Project
    |_ _ bin
      | _ _ your compiled classes
    |_ _ jars
      |_ _ testng.jar
      |_ _ jcommander.jar
      |_ _ bsh.jar
    |_ _ testng.xml

I have also written an article which explains how to run a testng.xml file while making a new project or in an existing project.我还写了一篇文章,解释了如何在创建新项目或在现有项目中运行 testng.xml 文件。 Hope you find your solution in the article.希望您在文章中找到您的解决方案。

Add this plugin if your project pom.xml如果你的项目 pom.xml 添加这个插件

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <reportsDirectory>./test-output</reportsDirectory>
                <suiteXmlFiles>
                    <!-- pass testng.xml files as argument from command line -->
                    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

after that use following command to pass the testng xml location:之后使用以下命令传递 testng xml 位置:

mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml

testng.xml should include details about test classes or packages to run. testng.xml 应包含有关要运行的测试类或包的详细信息。 Here is an example of content in testng.xml file:以下是 testng.xml 文件中的内容示例:

在此处输入图像描述

You can follow this link to create one https://www.softwaretestinghelp.com/testng-example-to-create-testng-xml/您可以按照此链接创建一个https://www.softwaretestinghelp.com/testng-example-to-create-testng-xml/

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

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