繁体   English   中英

如何使用注释在testng + maven中分离单元和集成测试?

[英]How to seperate unit and integration tests in testng + maven using annotations?

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故障安全插件完成相同的操作。 我想在测试类上使用注释将它们标记为集成测试。

这可以添加到测试中。

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

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

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

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

我们使用maven-surefire-plugin进行单元测试,使用maven-failsafe-plugin进行集成测试。 他们都很好地与Sonar融为一体。

看起来我迟到了这个派对,但对于未来的googlers,我通过以下方式让它工作:

使用您选择的组名称注释相关的测试

@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>

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

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM