我对maven很新,我想使用maven运行我的测试类。我已经生成了testng.xml,我也创建了POM.xml文件。但是当你运行mvn install
时,会产生这个错误:
[包org.testng.annotations不存在]
请告知此事。
pom.hml
<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”选项设置为“test”。
我如何修复它(注意,我使用了Eclipse):
删除测试范围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>
即使我遇到过这个问题,也得到了相同的解决方案。
在你的pom.xml文件中删除范围,这应该可以正常工作。
根据pom.xml中的以下代码,删除范围标记。
尽管我们已经为Testng导入了maven依赖项,但是当您在XML文件中添加范围标记时,它会将其视为JUnit注释而不是Testng。因此,当我删除范围标记时,My @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>
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>
如果您正在构建Web应用程序并且不希望它包含在WEB-INF / lib中,请将testng的范围设置为“已提供”。
确保您的测试位于“src / test / java”中。它会解决这个问题