简体   繁体   English

如何在NetBeans中安装TestNG?

[英]How to install TestNG in NetBeans?

First I added the dependency and repository to my project's maven pom.xml file: 首先,我将依赖项和存储库添加到项目的maven pom.xml文件中:

<repositories>
    <repository>
        <id>jcenter</id>
        <name>bintray</name>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

 <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.10</version>
    <scope>test</scope>
</dependency>

and did a NetBeans "Clean and Build". 并进行了NetBeans的“清理和构建”。 It downloaded TestNG stuff but project code still showed it couldn't find the classes it needed. 它下载了TestNG的东西,但是项目代码仍然显示找不到所需的类。 I can see that C:\\Users\\Greg.m2\\repository\\org\\testng\\testng\\6.10\\testng-6.10.jar exists. 我可以看到C:\\ Users \\ Greg.m2 \\ repository \\ org \\ testng \\ testng \\ 6.10 \\ testng-6.10.jar存在。 If I open that jar file I can see that org\\testng\\Assert.class exists. 如果打开该jar文件,则可以看到org \\ testng \\ Assert.class存在。 Why can't NetBeans find it? NetBeans为什么找不到它?

Since that failed, I followed instructions for setting up TestNG Environment here: https://www.tutorialspoint.com/testng/testng_environment.htm 既然失败了,我按照说明在这里设置TestNG Environment: https : //www.tutorialspoint.com/testng/testng_environment.htm

My TESTING_HOME environment variable is set to 我的TESTING_HOME环境变量设置为

C:\Users\Greg\Projects\TestNG

My CLASSPATH variable is set to 我的CLASSPATH变量设置为

%CLASSPATH%;%TESTING_HOME%\testng-6.10.jar

Yes, that jar file exists there. 是的,那个jar文件在那里。

I open up a new command shell and navigate to 我打开一个新的命令外壳并导航到

C:\Users\Greg\Projects\Enventive\en360\src

where there exists testng.xml 存在testng.xml

I then issue the following command per the TestNG author's doc 然后,我根据TestNG作者的文档发出以下命令

java org.testng.TestNG testng.xml

but get 但是得到

Error: Could not find or load main class org.testng.TestNG 错误:找不到或加载主类org.testng.TestNG

In NetBeans 8.2, I can create a TestNG class using 在NetBeans 8.2中,我可以使用以下方法创建一个TestNG类:

File>New File...>Unit Tests>TestNG Test Case

It produces test code but all the annotations have errors due to the error on the import line 它产生测试代码,但是由于导入行中的错误,所有注释都存在错误

import static org.testng.Assert.*;

which states 哪个州

package org.testng does not exist 软件包org.testng不存在

If I attempt to install the TestNG Plugin in NetBeans by downloading Maven TestNG Support and then trying to install it using 如果我尝试通过下载Maven TestNG支持来在NetBeans中安装TestNG插件,然后尝试使用

Tools>Plugins>Downloaded>[navigate to downloaded org-netbeans-modules-testng-maven.nbm]>Install

It fails to install with this error: 它无法安装,并显示以下错误:

Some plugins require plugin Maven Embedder to be installed. 一些插件需要安装插件Maven Embedder。 The plugin Maven Embedder is requested in version >= 2.54 but only 2.52.1 was found. 在版本> = 2.54中需要插件Maven Embedder,但仅找到2.52.1。 The following plugin is affected: Maven TestNG Support 以下插件受到影响:Maven TestNG支持

A search for "Maven Embedder" in "Available Plugins" tab, or on the web finds nothing. 在“可用插件”选项卡中或在网络上搜索“ Maven Embedder”,一无所获。

The problem was that the scope tag needs to be "compile", not "test". 问题在于,作用域标记需要“编译”,而不是“测试”。 The author's doc specifically says to use "test". 作者的文档专门说要使用“测试”。 Took me all day to find the solution here :-( 整日花费我在这里找到解决方案:-(

Maven Compilation error [package org.testng.annotations does not exist] Maven编译错误[软件包org.testng.annotations不存在]

While that fixed it for NetBeans, it did not for the command line. 尽管此问题适用于NetBeans,但不适用于命令行。 I managed to get that running by specifying the actual jar file in the -cp (classpath) parameter, like this: 我设法通过在-cp(classpath)参数中指定实际的jar文件来使其运行,如下所示:

java -cp C:\Users\Greg\Projects\MyCompany\MyProject\src;c:\users\Greg\Projects\TestNG\testng-6.10.jar org.testng.TestNG testng.xml

where the first path is the location of the testng.xml and the second path is to the jar file. 其中第一个路径是testng.xml的位置,第二个路径是jar文件。 Once again the author's doc is faulty in that it only shows a path to the folder containing the testng.xml. 作者的文档再次出现错误,因为它仅显示包含testng.xml的文件夹的路径。

Although this does find both the TestNG class and the testng.xml, an exception occurs: 尽管这确实找到了TestNG类和testng.xml,但是会发生异常:

Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException 线程“主”中的异常java.lang.NoClassDefFoundError:com / beust / jcommander / ParameterException

That seems strange because my testng.xml does not contain any parameters. 这似乎很奇怪,因为我的testng.xml不包含任何参数。 (Maybe it should?) Anyway, looking in the jar file, indeed, there is a com/beust but no jcommander folder. (也许应该吗?)无论如何,在jar文件中查找确实存在com / beust,但没有jcommander文件夹。

I downloaded the latest testng-6.14.3.jar and there is no com/beust/jcommander in that one either. 我下载了最新的testng-6.14.3.jar,并且该目录中也没有com / beust / jcommander。

Here is my testng.xml. 这是我的testng.xml。 Can anyone can see a problem? 谁能看到问题?

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="E360 Test Site" verbose="10"> 
  <test name="Test Analysis Results" preserve-order="true">
    <classes>
      <class name="test.java.core.EnProjectIT">
        <methods>
          <include name="testCreateTables" />
        </methods>
      </class>
    </classes>
  </test>
</suite>

To overcome this problem I added the following dependency to my pom.xml: 为了克服这个问题,我在pom.xml中添加了以下依赖项:

<dependency>
    <groupId>com.beust</groupId>
    <artifactId>jcommander</artifactId>
    <version>1.48</version>
</dependency>

Then added it to the classpath on the command line like this: java -cp C:\\Users\\Greg\\Projects\\Enventive\\en360\\src;c:\\users\\Greg\\Projects\\TestNG\\testng-6.14.3.jar;c:\\users\\Greg\\Projects\\TestNG\\jcommander-1.72.jar org.testng.TestNG testng.xml 然后像下面这样将其添加到命令行的类路径中:java -cp C:\\Users\\Greg\\Projects\\Enventive\\en360\\src;c:\\users\\Greg\\Projects\\TestNG\\testng-6.14.3.jar;c:\\users\\Greg\\Projects\\TestNG\\jcommander-1.72.jar org.testng.TestNG testng.xml

Now I get the following error: 现在我得到以下错误:

Cannot find class in classpath: test.java.core.EnProjectIT 在类路径中找不到类:test.java.core.EnProjectIT

Ok, so I need to add the compiled class location, not the java code location to the classpath. 好的,因此我需要将已编译的类位置而不是Java代码位置添加到类路径中。 Now it looks like this: 现在看起来像这样:

java -cp C:\\Users\\Greg\\Projects\\MyCompany\\MyProject\\src;C:\\Users\\Greg\\Projects\\MyCompany\\MyProject\\target\\classes;c:\\users\\Greg\\Projects\\TestNG\\testng-6.14.3.jar;c:\\users\\Greg\\Projects\\TestNG\\jcommander-1.72.jar org.testng.TestNG testng.xml

New error is: 新错误是:

Cannot instantiate class test.java.core.EnProjectIT Caused by: java.lang.NoClassDefFoundError: org/kohsuke/args4j/CmdLineException 无法实例化类test.java.core.EnProjectIT原因:java.lang.NoClassDefFoundError:org / kohsuke / args4j / CmdLineException

Seems I am just kicking the can down the road. 似乎我只是在踢罐子。 Maybe I need to put my whole .m2 repo in the classpath? 也许我需要将整个.m2回购放入类路径中?

Ok, I found a clever way to fix all these dependency issues. 好的,我找到了解决所有这些依赖性问题的聪明方法。 Since NetBeans was able to build the project ok, it must have a way of gathering all the jar files into its classpath. 由于NetBeans可以正常构建项目,因此它必须具有一种将所有jar文件收集到其类路径中的方法。 Sure enough, I found all of them in one location: C:\\Users\\Greg\\Projects\\MyCompany\\MyProject\\target\\ProductSnapshot\\WEB-INF\\lib* 果然,我在一个位置找到了所有这些文件:C:\\ Users \\ Greg \\ Projects \\ MyCompany \\ MyProject \\ target \\ ProductSnapshot \\ WEB-INF \\ lib *

I added that to the -cp which now looks like this: 我将其添加到-cp中,现在看起来像这样:

java -cp C:\Users\Greg\Projects\MyCompany\MyProject\src;C:\Users\Greg\Projects\MyCompany\MyProject\target\classes;C:\Users\Greg\Projects\MyCompany\MyProject\target\ProductSnapshot\WEB-INF\lib\* org.testng.TestNG testng.xml

Well I think I'm very close now. 好吧,我想我现在很接近。 Next error says it can't find a project file. 下一个错误表明它找不到项目文件。 It looks like I need to run the program from a different directory. 看来我需要从其他目录运行该程序。

java.io.FileNotFoundException: C:\\Users\\Greg\\Projects\\MyCompany\\MyProject\\src\\WEB-INF\\EnDatabaseTest.properties (The system cannot find the path specified) java.io.FileNotFoundException:C:\\ Users \\ Greg \\ Projects \\ MyCompany \\ MyProject \\ src \\ WEB-INF \\ EnDatabaseTest.properties(系统找不到指定的路径)

It's appending WEB-INF\\EnDatabaseTest.properties to the startup directory so I will make the startup directory C:\\Users\\Greg\\Projects\\MyCompany\\MyProduct\\WebContent 它将WEB-INF \\ EnDatabaseTest.properties附加到启动目录,因此我将启动目录设为C:\\ Users \\ Greg \\ Projects \\ MyCompany \\ MyProduct \\ WebContent

I put the testng.xml in that directory, cd'd to that directory, and ran this: 我将testng.xml放在该目录中,CD到该目录,然后运行:

java -cp C:\Users\Greg\Projects\MyCompany\MyProject\WebContent;C:\Users\Greg\Projects\MyCompany\MyProject\target\classes;C:\Users\Greg\Projects\MyCompany\MyProject\target\En360-1.0.0\WEB-INF\lib\* org.testng.TestNG testng.xml

And it worked! 而且有效!

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

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