Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在testng和x2B中分离单元测试和集成测试;maven使用注释?_Java_Maven_Integration Testing_Testng - Fatal编程技术网

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

Java 如何在testng和x2B中分离单元测试和集成测试;maven使用注释?,java,maven,integration-testing,testng,Java,Maven,Integration Testing,Testng,maven fail-safe插件需要能够区分单元测试和集成测试之间的区别。似乎在使用JUnit时,分离测试的一种方法是使用JUnit@Categories注释。这篇博客文章展示了如何使用junit实现这一点 如何使用TestNG和Maven failsafe插件完成同样的事情。我想在测试类上使用注释将它们标记为集成测试。这可以添加到测试中 @IfProfileValue(name="test-profile", value="IntegrationTest") public class Pen

maven fail-safe插件需要能够区分单元测试和集成测试之间的区别。似乎在使用JUnit时,分离测试的一种方法是使用JUnit@Categories注释。这篇博客文章展示了如何使用junit实现这一点


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

这可以添加到测试中

@IfProfileValue(name="test-profile", value="IntegrationTest")
public class PendingChangesITCase extends AbstractControllerIntegrationTest {
    ...
}
<properties>
    <test-profile>IntegrationTest</test-profile>
</properties>
要选择要执行的测试,只需将值添加到概要文件以执行集成测试

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

集成测试

如果选择的maven概要文件没有属性值,它将不会执行集成测试。

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

看来我参加这次聚会迟到了,但对于未来的谷歌用户来说,我是通过以下方式实现的:

<properties>
    <test-profile>IntegrationTest</test-profile>
</properties>
使用您选择的组名注释相关测试类:

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

org.apache.maven.plugins
maven surefire插件
我的集成测试
并告诉故障保护插件(运行集成测试)只关心您的组

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

org.apache.maven.plugins
maven故障保护插件
2.20
集成测试
验证
**/*.爪哇
我的集成测试

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