如何从命令行运行 TestNG [英] How to run TestNG from command line

查看:56
本文介绍了如何从命令行运行 TestNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我究竟如何从命令行运行 .java TestNG 项目?

我已经通读了 TestNG 文档,并尝试了以下方法无济于事:

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

... 在我的项目中使用以下 testng.xml 文件:

<!DOCTYPE套件系统http://testng.org/testng-1.0.dtd"><套件名称="SuiteAll" verbose="1"><测试名称="TestAll"><包><包名称="com.project.fred.tests"/></包></测试></套房>

我得到的错误是这样的:

线程main"中的异常 java.lang.NoClassDefFoundError: org/testng/TestNG引起:java.lang.ClassNotFoundException:org.testng.TestNG在 java.net.URLClassLoader$1.run(Unknown Source)在 java.security.AccessController.doPrivileged(Native Method)在 java.net.URLClassLoader.findClass(Unknown Source)在 java.lang.ClassLoader.loadClass(来源不明)在 sun.misc.Launcher$AppClassLoader.loadClass(来源不明)在 java.lang.ClassLoader.loadClass(来源不明)找不到主类:org.testng.TestNG.程序将会退出.

显然,我没有在命令行中正确引用 TestNG.有谁知道如何让这个工作?

解决方案

你需要在classpath下有testng.jar.

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

更新:

在 linux 下我运行了这个命令,它在 Windows 上也有类似的东西

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

目录结构:

/bin - 我所有的测试包都在 bin 下,包括 testng.xml/src - 所有源文件都在 src 下/lib - 执行测试所需的所有库都在此之下.

一旦我编译了所有源代码,它们就会进入 bin 目录下.因此,在类路径中,我需要在此处指定 bin 目录和所有库(如 testng.xml、loggers 等)的内容.如果您不想指定 testng.xml 可用的完整路径,也将 testng.xml 复制到 bin 文件夹.

/bin-- testng.xml-- 测试类-- 属性文件(如果有)./lib-- testng.jar-- log4j.jar

更新:

转到文件夹 MyProject 并键入运行 java 命令,如下所示:-

java -cp ".: C:Program Filesjbdevstudio4studioplugins*" org.testng.TestNG testng.xml

我相信 testng.xml 文件在 C:UsersmeworkspaceMyProject 下,如果不是,请提供 testng.xml 文件的完整路径>

How exactly do I run a .java TestNG project from a command line?

I have read through the TestNG documentation, and tried the following to no avail:

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

... with the following testng.xml file in my project:

<!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. Does anyone know how to get this working?

解决方案

You need to have the testng.jar under classpath.

try 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

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. So, in the classpath I need to specify contents of bin directory and all the libraries like testng.xml, loggers etc over here. Also copy testng.xml to bin folder if you dont want to specify the full path where the testng.xml is available.

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

Update:

Go to the folder MyProject and type run the java command like the way shown below:-

java -cp ".: C:Program Filesjbdevstudio4studioplugins*" org.testng.TestNG testng.xml

I believe the testng.xml file is under C:UsersmeworkspaceMyProject if not please give the full path for testng.xml file

这篇关于如何从命令行运行 TestNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆