1、配置项目pom文件

Cucumber的启动类配置到pom文件中,在Jenkins构建的时候,会跑Cucumber测试类,配置如下:

  1. <profiles>  
  2.     <profile>  
  3.         <id>cucumber</id>  
  4.         <!-- 仅运行cucumber测试 -->  
  5.         <build>  
  6.             <plugins>  
  7.                 <plugin>  
  8.                     <groupId>org.apache.maven.plugins</groupId>  
  9.                     <artifactId>maven-surefire-plugin</artifactId>  
  10.                     <configuration>  
  11.                         <disableXmlReport>true</disableXmlReport>  
  12.                         <includes>  
  13.                             <include>**/cucumber启动类.java</include>  
  14.                         </includes>  
  15.                     </configuration>  
  16.                 </plugin>  
  17.                 <plugin>  
  18.                     <groupId>org.apache.maven.plugins</groupId>  
  19.                     <artifactId>maven-dependency-plugin</artifactId>  
  20.                     <version>2.10</version>  
  21.                     <executions>  
  22.                         <execution>  
  23.                             <id>复制测试时用到的工具</id><!-- 由于我的测试中使用了aop,所以需要加这个配置 -->  
  24.                             <phase>process-resources</phase>  
  25.                             <goals>  
  26.                                 <goal>copy</goal>  
  27.                             </goals>  
  28.                             <configuration>  
  29.                                 <artifactItems>  
  30.                                     <artifactItem>  
  31.                                         <groupId>org.aspectj</groupId>  
  32.                                         <artifactId>aspectjweaver</artifactId>  
  33.                                         <version>1.8.5</version>  
  34.                                     </artifactItem>  
  35.                                 </artifactItems>  
  36.                                 <stripVersion>true</stripVersion>  
  37.                             </configuration>  
  38.                         </execution>  
  39.                         <execution>  
  40.                             <id>获得jar在m2中的路径,供plugin引用</id>  
  41.                             <goals>  
  42.                                 <goal>properties</goal>  
  43.                             </goals>  
  44.                         </execution>  
  45.                     </executions>  
  46.                 </plugin>  
  47.             </plugins>  
  48.             <pluginManagement>  
  49.                 <plugins>  
  50.                     <plugin>  
  51.                         <groupId>org.apache.maven.plugins</groupId>  
  52.                         <artifactId>maven-surefire-plugin</artifactId>  
  53.                         <version>2.17</version>  
  54.                     </plugin>  
  55.                     <plugin>  
  56.                         <groupId>org.jacoco</groupId>  
  57.                         <artifactId>jacoco-maven-plugin</artifactId>  
  58.                         <version>0.7.4.201502262128</version>  
  59.                         <!-- 要和sonar里面的保持一致 -->  
  60.                     </plugin>  
  61.                     <plugin>  
  62.                         <groupId>org.eclipse.m2e</groupId>  
  63.                         <artifactId>lifecycle-mapping</artifactId>  
  64.                         <version>1.0.0</version>  
  65.                         <configuration>  
  66.                             <lifecycleMappingMetadata>  
  67.                                 <pluginExecutions>  
  68.                                     <pluginExecution>  
  69.                                         <pluginExecutionFilter>  
  70.                                             <groupId>org.apache.maven.plugins</groupId>  
  71.                                             <artifactId>maven-dependency-plugin</artifactId>  
  72.                                             <versionRange>[2.0,)</versionRange>  
  73.                                             <goals>  
  74.                                                 <goal>copy-dependencies</goal>  
  75.                                                 <goal>unpack</goal>  
  76.                                                 <goal>copy</goal>  
  77.                                             </goals>  
  78.                                         </pluginExecutionFilter>  
  79.                                         <action>  
  80.                                             <ignore />  
  81.                                         </action>  
  82.                                     </pluginExecution>  
  83.                                     <pluginExecution>  
  84.                                         <pluginExecutionFilter>  
  85.                                             <groupId>org.assertj</groupId>  
  86.                                             <artifactId>  
  87.                                                 assertj-assertions-generator-maven-plugin  
  88.                                             </artifactId>  
  89.                                             <versionRange>  
  90.                                                 [1.6.0,)  
  91.                                             </versionRange>  
  92.                                             <goals>  
  93.                                                 <goal>  
  94.                                                     generate-assertions  
  95.                                                 </goal>  
  96.                                             </goals>  
  97.                                         </pluginExecutionFilter>  
  98.                                         <action>  
  99.                                             <ignore></ignore>  
  100.                                         </action>  
  101.                                     </pluginExecution>  
  102.                                     <pluginExecution>  
  103.                                         <pluginExecutionFilter>  
  104.                                             <groupId>org.jacoco</groupId>  
  105.                                             <artifactId>  
  106.                                                 jacoco-maven-plugin  
  107.                                             </artifactId>  
  108.                                             <versionRange>  
  109.                                                 [0.7.4.201502262128,)  
  110.                                             </versionRange>  
  111.                                             <goals>  
  112.                                                 <goal>prepare-agent</goal>  
  113.                                             </goals>  
  114.                                         </pluginExecutionFilter>  
  115.                                         <action>  
  116.                                             <ignore></ignore>  
  117.                                         </action>  
  118.                                     </pluginExecution>  
  119.                                 </pluginExecutions>  
  120.                             </lifecycleMappingMetadata>  
  121.                         </configuration>  
  122.                     </plugin>  
  123.                 </plugins>  
  124.             </pluginManagement>  
  125.         </build>  
  126.     </profile>  
  127. </profiles>  
<profiles>
    <profile>
        <id>cucumber</id>
        <!-- 仅运行cucumber测试 -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <disableXmlReport>true</disableXmlReport>
                        <includes>
                            <include>**/cucumber启动类.java</include>
                        </includes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.10</version>
                    <executions>
                        <execution>
                            <id>复制测试时用到的工具</id><!-- 由于我的测试中使用了aop,所以需要加这个配置 -->
                            <phase>process-resources</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>org.aspectj</groupId>
                                        <artifactId>aspectjweaver</artifactId>
                                        <version>1.8.5</version>
                                    </artifactItem>
                                </artifactItems>
                                <stripVersion>true</stripVersion>
                            </configuration>
                        </execution>
                        <execution>
                            <id>获得jar在m2中的路径,供plugin引用</id>
                            <goals>
                                <goal>properties</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.17</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.7.4.201502262128</version>
                        <!-- 要和sonar里面的保持一致 -->
                    </plugin>
                    <plugin>
                        <groupId>org.eclipse.m2e</groupId>
                        <artifactId>lifecycle-mapping</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <lifecycleMappingMetadata>
                                <pluginExecutions>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-dependency-plugin</artifactId>
                                            <versionRange>[2.0,)</versionRange>
                                            <goals>
                                                <goal>copy-dependencies</goal>
                                                <goal>unpack</goal>
                                                <goal>copy</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore />
                                        </action>
                                    </pluginExecution>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>org.assertj</groupId>
                                            <artifactId>
                                                assertj-assertions-generator-maven-plugin
                                            </artifactId>
                                            <versionRange>
                                                [1.6.0,)
                                            </versionRange>
                                            <goals>
                                                <goal>
                                                    generate-assertions
                                                </goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore></ignore>
                                        </action>
                                    </pluginExecution>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>org.jacoco</groupId>
                                            <artifactId>
                                                jacoco-maven-plugin
                                            </artifactId>
                                            <versionRange>
                                                [0.7.4.201502262128,)
                                            </versionRange>
                                            <goals>
                                                <goal>prepare-agent</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore></ignore>
                                        </action>
                                    </pluginExecution>
                                </pluginExecutions>
                            </lifecycleMappingMetadata>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

注意cucumber启动类.Java,这个java类的作用是指定features文件的位置,步骤定义的位置,以及生成的测试报告存放的问题,该类代码如下:

  1. @RunWith(Cucumber.class)  
  2.   
  3. @CucumberOptions(plugin={"pretty""html:target/cucumber""json:target/cucumber.json""junit:target/junit"},glue={"com.cucumber"}, features={"src/test/resources/features/"})  
  4. public class cucumber启动类 {  
  5. public static void main(String[] args) throws IOException {  
  6.         Main.run(args, Thread.currentThread().getContextClassLoader());  
  7.     }  
  8. }  
@RunWith(Cucumber.class)

@CucumberOptions(plugin={"pretty", "html:target/cucumber", "json:target/cucumber.json", "junit:target/junit"},glue={"com.cucumber"}, features={"src/test/resources/features/"})
public class cucumber启动类 {
public static void main(String[] args) throws IOException {
        Main.run(args, Thread.currentThread().getContextClassLoader());
    }
}

  1. 注:plugin选项用来指定生成的报告格式,多种格式用逗号隔开,glue用来指定cucumber的步骤定义位置,features用来指定features文件的位置  
注:plugin选项用来指定生成的报告格式,多种格式用逗号隔开,glue用来指定cucumber的步骤定义位置,features用来指定features文件的位置

2、添加测试构建插件配置

  1. <plugin>  
  2.                 <groupId>org.codehaus.mojo</groupId>  
  3.                 <artifactId>javancss-maven-plugin</artifactId>  
  4.                 <version>2.1</version>  
  5.                 <configuration>  
  6.                     <failOnViolation>true</failOnViolation>  
  7.                     <lineThreshold>10000</lineThreshold>  
  8.                     <ccnLimit>12</ccnLimit>  
  9.                     <ncssLimit>90</ncssLimit>  
  10.                     <includes>  
  11.                         <include>**/*.java</include>  
  12.                     </includes>  
  13.                     <excludes>  
  14.                         <exclude>**/bar.java</exclude>  
  15.                         <exclude>**/foobar.java</exclude>  
  16.                     </excludes>  
  17.                 </configuration>  
  18.             </plugin>  
  19.             <plugin>  
  20.                 <groupId>org.apache.maven.plugins</groupId>  
  21.                 <artifactId>maven-surefire-plugin</artifactId>  
  22.                 <executions>  
  23.                     <execution>  
  24.                         <id>run-cucumber</id>  
  25.                         <goals>  
  26.                             <goal>test</goal>  
  27.                         </goals>  
  28.                         <phase>test</phase>  
  29.                         <configuration>  
  30.                             <disableXmlReport>false</disableXmlReport>  
  31.                             <includes>  
  32.                                 <include>**/cucumber启动类.java</include>  
  33.                             </includes>  
  34.                         </configuration>  
  35.                     </execution>  
  36.                 </executions>  
  37.                 <configuration>  
  38.                     <testFailureIgnore>  
  39.                         true<!-- 因为要执行Cucumber -->  
  40.                     </testFailureIgnore>  
  41.                 </configuration>  
  42.             </plugin>  
<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>javancss-maven-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<failOnViolation>true</failOnViolation>
					<lineThreshold>10000</lineThreshold>
					<ccnLimit>12</ccnLimit>
					<ncssLimit>90</ncssLimit>
					<includes>
						<include>**/*.java</include>
					</includes>
					<excludes>
						<exclude>**/bar.java</exclude>
						<exclude>**/foobar.java</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<executions>
					<execution>
						<id>run-cucumber</id>
						<goals>
							<goal>test</goal>
						</goals>
						<phase>test</phase>
						<configuration>
							<disableXmlReport>false</disableXmlReport>
							<includes>
								<include>**/cucumber启动类.java</include>
							</includes>
						</configuration>
					</execution>
				</executions>
				<configuration>
					<testFailureIgnore>
						true<!-- 因为要执行Cucumber -->
					</testFailureIgnore>
				</configuration>
			</plugin>

3、在Jenkins中安装cucumber插件

需要安装的插件如下:


4、新建一个Jenkins项目,并配置

配置如下:




5、点击应用保存,并构建

6、构建完成后,效果如下

构建完之后,会多生成这两个链接,点击Cucumber Reports后就可以看到好看的测试报告了,下面是部分截图:



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐