我是maven的新手,我想使用maven运行我的测试类。我已经生成了testng.xml,还创建了POM.xml文件。但是当您运行mvn install
时,它会生成以下错误:
[程序包org.testng.annotifications不存在]
请对此提出建议。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.TestNG</groupId>
<artifactId>TestNG</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="1" preserve-order="true">
<test name="Test">
<packages>
<package name="com.testngTest2" />
<package name="com.testngTest" />
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->
我也遇到了类似的问题。原因是"testng"依赖项的"Scope"选项在需要"compile"时设置为"test"。
我是如何修复的(注意,我使用了Eclipse):
- 打开pom.xml文件
- 转到"依赖项"选项卡
- 选择"testng"软件包,然后单击"属性…
- 在打开的屏幕上,将"Scope"选项更改为"compile(编译)",然后单击"OK"保存
- 试着用"编译测试"的目标重新构建你的项目
删除测试范围testng依赖项并添加编译
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>compile</scope>
</dependency>
在pom.xml文件中,testng的范围为test
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
<scope>test</scope>
</dependency>
用编译替换作用域
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
<scope>compile</scope>
</dependency>
Beleive me below wil work replace "test" to "compile" in scope
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
<scope>compile</scope>
</dependency>
甚至我也遇到过这个问题,并得到了同样的解决方案。
在pom.xml文件中删除作用域,这应该可以正常工作。
根据pom.xml中的以下代码,删除scope标记。
尽管我们已经为Testng导入了maven依赖项,但当您在XML文件中添加scope标记时,它将被视为JUnit注释,而不是Testng。因此,当我删除scope标记时,我的@Test Annotation被视为Testng Annotation。
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
**<scope>test</scope>** //Remove this line and compile maven
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>compile</scope>
</dependency>
确保您的测试在"src/test/java"中。它将解决这个问题
如果您正在构建一个web应用程序,并且不希望它包含在web-INF/lib中,请将testng的作用域设置为"provided"。
以下解决方案适用于我-
请在pom.xml 中添加以下依赖项
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
对于找不到pom.xml文件的JetBrains IntelliJ用户:
- 转到"文件">'项目结构
- 从"项目设置">中选择测试包所在的模块模块
- 转到"依赖项"选项卡
- 将作用域设置从"test"更改为"compile"