【问题标题】:TestNG annotations are not respected when run group based tests through command line通过命令行运行基于组的测试时,不考虑 TestNG 注释
【发布时间】:2015-11-04 15:02:00
【问题描述】:

我想使用 Maven 通过命令行基于他们的组运行我的测试。(意味着如果我说mvn test -Dgroups=sanity,则只会运行标记为sanity 组的测试)

为了实现上述目标,我在 pom.xml 中编写了以下代码。因为如果没有在命令行上为组(即mvn test)提供任何内容,它将使用regression 组运行测试

<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>org.maven.test</groupId>
    <artifactId>MavenTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <groups>regression</groups>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.47.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <configuration>
                        <groups>${groups}</groups>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

我的测试类有BeforeClassAfterClass 注释:

public class SanityTest {
  @Test(groups={"sanity"})
  public void f() {
    System.out.println("This is sanity test");
  }

  @BeforeClass
  public void beforeClass() {
    System.out.println("This is before class.");
  }

  @AfterClass
  public void afterClass() {
    System.out.println("This is after class.");
  }
}

为了运行健全性测试,我点击了mvn test -Dgroups=sanity。健全性测试会运行,但不会执行 AfterClassBeforeClass 方法。

它的输出是:

This is sanity test

实际上,我在这里期待:

This is before class.
This is sanity test
This is after class.

有人可以帮我做什么,以便同时尊重 TestNG 注释吗?

【问题讨论】:

  • 显示完整的 pom 文件,请添加完整的输出 mvn clean test ..
  • khmarbaise,我用完整的 pom.xml 更新了问题。我应该添加mvn clean testmvn clean test -Dgroups=sanity 的输出吗?

标签: java maven selenium-webdriver testng maven-surefire-plugin


【解决方案1】:

您需要将 alwaysRun 属性添加到您的 @BeforeClass 和 @AfterClass 注释中:

来自 testNG 文档: alwaysRun 对于 before 方法(beforeSuite、beforeTest、beforeTestClass 和 beforeTestMethod,但不包括 beforeGroups):如果设置为 true,则无论属于哪个组,都会运行此配置方法。 对于 after 方法(afterSuite、afterClass、...):如果设置为 true,即使之前调用的一个或多个方法失败或被跳过,也会运行此配置方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2016-11-03
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多