【问题标题】:How to seperate unit and integration tests in testng + maven using annotations?如何使用注释在 testng + maven 中分离单元测试和集成测试?
【发布时间】:2013-10-23 00:27:11
【问题描述】:

maven 故障安全插件需要能够区分单元测试和集成测试。似乎在使用 JUnit 时,一种分离测试的方法是使用 JUnit @Categories 注释。这篇博文展示了如何使用junit http://www.agile-engineering.net/2012/04/unit-and-integration-tests-with-maven.html

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

 @Test
 public void longRunningServiceTest() throws Exception {

 }
}

如何使用 TestNG 和 Maven 故障安全插件完成同样的事情。我想在测试类上使用注释将它们标记为集成测试。

【问题讨论】:

  • 为什么要使用注释而不是命名约定?此外,在大多数情况下,最好有一个包含集成测试的单独 maven 模块。您可能会改用 TestNG 中的组信息,但我认为这是错误的方式(我的观点)。
  • @khmarbaise 我不想拥有单独的集成测试模块,因为它会在我的应用程序中创建大量模块。注释使 Eclipse 中可以轻松找到所有使用集成测试注释标记的类。我可以定义自己的更专业的注释,例如需要运行 tomcat 服务器的 rest api 集成测试,vs selenium 测试,vs 需要数据库服务器但没有 tomcat 服务器的服务集成测试......等等。简而言之,注释是更灵活。

标签: java maven integration-testing testng


【解决方案1】:

看来我参加这个聚会迟到了,但对于未来的谷歌员工,我通过以下方式让它发挥作用:

用您选择的组名注释相关的测试类:

@Test(groups='my-integration-tests')
public class ExampleIntegrationTest {
  @Test
  public void someTest() throws Exception {

 }
}

告诉surefire插件(运行正常的单元测试阶段)忽略您的集成测试:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <excludedGroups>my-integration-tests</excludedGroups>
  </configuration>
</plugin>

并告诉故障安全插件(运行集成测试)只关心您的组。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.20</version>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>**/*.java</includes>
    <groups>my-integration-tests</groups>
  </configuration>
</plugin>

【讨论】:

    【解决方案2】:

    我们使用 maven-surefire-plugin 进行单元测试,使用 maven-failsafe-plugin 进行集成测试。它们都与声纳很好地集成在一起。

    【讨论】:

      【解决方案3】:

      这可以添加到测试中。

      @IfProfileValue(name="test-profile", value="IntegrationTest")
      public class PendingChangesITCase extends AbstractControllerIntegrationTest {
          ...
      }
      

      要选择要执行的测试,只需将值添加到配置文件以执行集成测试。

      <properties>
          <test-profile>IntegrationTest</test-profile>
      </properties>
      

      如果所选的 maven 配置文件没有属性值,则不会执行集成测试。

      【讨论】:

        猜你喜欢
        • 2016-01-23
        • 2011-12-11
        • 1970-01-01
        • 2014-11-15
        • 2016-10-03
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 2013-03-18
        相关资源
        最近更新 更多