Maven编译错误[包org.testng.annotations不存在]

use*_*386 16 testng pom.xml maven

我对maven很新,我想使用maven运行我的测试类.我已经生成了testng.xml,我也创建了POM.xml文件.但是当你运行mvn install它时,会产生这个错误:

[包org.testng.annotations不存在]

请就此提出建议.

的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>
Run Code Online (Sandbox Code Playgroud)

的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 -->
Run Code Online (Sandbox Code Playgroud)

小智 51

我有类似的问题.原因是当需要"编译"时,"testng"依赖的"Scope"选项设置为"test".

我如何修复它(注意,我使用了Eclipse):

  1. 打开pom.xml文件.
  2. 转到" 依赖关系 "选项卡.
  3. 选择" testng "包并单击" Properties ... "
  4. 在打开的屏幕上,将" 范围 "选项更改为" 编译 ",然后单击"确定"以保存它.
  5. 尝试使用"编译测试"目标再次构建项目.


neo*_*rol 19

删除测试范围testng依赖项并添加编译

<dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>compile</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)


Anu*_*tia 9

在pom.xml文件中,testng的范围为test

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

通过编译替换范围

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)


小智 5

即使我遇到过这个问题,也得到了相同的解决方案.

在你的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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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>
Run Code Online (Sandbox Code Playgroud)