【问题标题】:Test cases in TestNG are not executed by mavenTestNG中的测试用例不是由maven执行的
【发布时间】:2020-01-09 09:13:22
【问题描述】:

我有一个带有 testng 脚本的 maven 项目,如下所示:

package TestMaven;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class mavenTestDemo {

@Test
public void add() {
    System.out.println("Addintion");
    int a=10;
    int b=20;
    assertEquals(30, a+b);
}

@Test
public void sub() {
    System.out.println("Subtraction");
    int a=10;
    int b=20;
    assertEquals(10, b-a);
}

@Test
public void mult() {
    System.out.println("Multiplication");
    int a=10;
    int b=20;
    assertEquals(200, a*b);
}


}

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>MavenDemo</groupId>
  <artifactId>MavenDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 </properties>
  <dependencies>
     <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>compile</scope>
        </dependency>


  </dependencies>
</project>

当我通过 maven 执行代码时,我得到以下输出: 我使用“maven test”选项直接从 eclipse 执行,并使用命令“mvn clean install”从命令行尝试 对于这两种执行,我的结果都低于结果--->

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< MavenDemo:MavenDemo >-------------------------
[INFO] Building MavenDemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Shilpa Khandge\eclipse-workspace\MavenDemo\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Shilpa Khandge\eclipse-workspace\MavenDemo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.900 s
[INFO] Finished at: 2020-01-09T14:33:56+05:30
[INFO] ------------------------------------------------------------------------

【问题讨论】:

    标签: maven selenium


    【解决方案1】:

    请在您的 pom.xml 中添加 maven surefire 插件,该插件应指向 testng.xml

    <plugins>
    
     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
            <configuration>
              <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
              </suiteXmlFiles>
            </configuration>
          </plugin>
    
    </plugins>
    

    然后运行mvn install

    【讨论】:

    • 问题已解决。谢谢大家的投入:)
    【解决方案2】:

    为了让 TestNg 识别 @Test 方法,类名应该以 Test 结尾。

    在您的情况下,将类名从 mavenTestDemo 重命名为 MavenTest 或以 Test 结尾的任何其他名称。

    否则,您需要有一个包含测试类的 testng.xml 文件。

    【讨论】:

      【解决方案3】:

      您需要有一个 Testng.xml 并定义测试类并通过 mvn clean install 运行重新运行测试。 确保你在 pom.xml surefire 插件中定义了 testng.xml。这样 maven 会调用您的 testng.xml,然后依次指向测试方法。

      【讨论】:

        猜你喜欢
        • 2019-05-08
        • 2017-06-17
        • 2016-03-04
        • 2017-01-09
        • 1970-01-01
        • 2020-06-04
        • 2016-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多