美文网首页
带命令行参数的 mvn integration-test

带命令行参数的 mvn integration-test

作者: DongGuangqing | 来源:发表于2016-12-29 21:57 被阅读362次

    背景

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

    • pom 中的property 配置
      <properties>
        <instance.type>Redis</instance.type>
      </properties>
    
    • pom 中的plugin 配置
          <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>
    
    • 代码中使用命令行参数instanceType
        @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 实现

    • pom 中的plugin 配置
        <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>
    
    • 代码中使用命令行参数instanceType
      @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
    

    相关文章

      网友评论

          本文标题:带命令行参数的 mvn integration-test

          本文链接:https://www.haomeiwen.com/subject/rkvpvttx.html