带命令行参数的 mvn integration-test

2016-12-29  本文已影响362人  DongGuangqing

背景

  1. 使用 maven-failsafe-plugin 进行集成测试
  2. 运行: mvn integration-test 执行完所有的继承测试(在@BeforeClass 中初始化环境, 在@AfterClass 中清理环境)

需求

在 mvn integration-test 命令后面增加参数,在此设为instanceType。 若instanceType=Redis 则在@BeforeClass 中创建一个Redis实例; 若instanceType=Memcache 则在@BeforeClass 中创建一个Memcache 实例

PS: 当然也可以写一份Redis 的测试代码,一份Memcache的测试代码,但是考虑到两者大部分的测试代码都是相同的,会造成大量代码冗余,因此不考虑

实现

总体来说是在maven-failsafe-plugin 的configuration 中进行配置

基于environmentVariables 实现

  <properties>
    <instance.type>Redis</instance.type>
  </properties>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
          <execution>
            <id>integration-tests</id>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
            <configuration>
              <!-- use mvn integration-test -Dinstance.type=Memcache to pass command line param -->
              <environmentVariables>
                <instanceType>${instance.type}</instanceType>
              </environmentVariables>
              <includes>
                <include>com/aliyun/open/api/redisa/TestSuites.class</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    @BeforeClass
    public static void suitesSetup() {
        String dbInstanceId = null
        if ("Memcache".equalsIgnoreCase(System.getenv("instanceType"))) {
            // create memcache instance
        } else {
            // create redis instance
        }
    }
mvn integration-test -Dinstance.type=Memcache

基于systemPropertyVariables 实现

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
          <execution>
            <id>integration-tests</id>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
            <configuration>
              <!-- another way to pass command line param -->
              <systemPropertyVariables>
                <instanceType>Redis</instanceType>
              </systemPropertyVariables>
              <includes>
                <include>com/aliyun/open/api/redisa/TestSuites.class</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
  @BeforeClass
  public static void suitesSetup() {
      String dbInstanceId = null
      if ("Memcache".equalsIgnoreCase(System.getProperty("instanceType"))) {
          // create memcache instance
      } else {
          // create redis instance
      }
  }
mvn integration-test -DinstanceType=Memcache
上一篇下一篇

猜你喜欢

热点阅读