文章为原创,未经本人授权禁止转载。

一、spock框架环境搭建。

二、基于spock框架的脚本开发。

三、基于spock框架的用例执行并生成HTML报告。

四、集成jenkins生成HTML报告。

五、通过tomcat访问HTML报告。

一、spock框架环境搭建

1、插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
<!-- Optional plugins for using Spock -->
<!-- Only required if names of spec classes don't match default Surefire patterns (`*Test` etc.) -->

<!--使用mvn test命令生成xml和txt结果文件-->
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <includes>
            <!--指定需要执行的测试类路径-->
            <include>themis/shenpijieguo/*Spec</include>
        </includes>
    </configuration>
</plugin>
<!-- Mandatory plugins for using Spock -->
<plugin>
    <!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
    visit https://github.com/groovy/GMavenPlus/wiki -->
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<!-- 使用mvn test surefire-report:report 将先生成xml和TXT结果文件,再将xml结果文件转化为HTML报告,该报告排版不好-->
<!--<plugin>-->
    <!--<groupId>org.apache.maven.plugins</groupId>-->
    <!--<artifactId>maven-surefire-report-plugin</artifactId>-->
    <!--<version>2.20</version>-->
    <!--<configuration>-->
        <!--<showSuccess>true</showSuccess>-->
    <!--</configuration>-->
<!--</plugin>-->
<!-- Only required for spock-example build -->
<!--<plugin>-->
    <!--<artifactId>maven-deploy-plugin</artifactId>-->
    <!--<version>2.5</version>-->
    <!--<configuration>-->
        <!--<skip>true</skip>-->
    <!--</configuration>-->
<!--</plugin>-->

<!-- 用mvn ant生成格式更友好的report -->

<!-- 执行命令为mvn test生成格式更好的HTML报告 -->
<plugin>
    <groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
    <artifactId>maven-antrun-extended-plugin</artifactId>   <!-- //////////// -->
    <executions>
        <execution>
            <id>test-reports</id>
            <phase>test</phase<!-- //////////// -->
            <configuration>
                <tasks>
                    <junitreport todir="${basedir}/target/surefire-reports">
                        <fileset dir="${basedir}/target/surefire-reports">
                            <include name="**/*.xml" />
                        </fileset>
                        <report format="frames" todir="${basedir}/target/surefire-reports" /> <!-- //////////// -->
                    </junitreport>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-junit</artifactId>
            <version>1.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-trax</artifactId>
            <version>1.8.0</version>
        </dependency>
    </dependencies>
</plugin>

2、依赖

<!-- Mandatory dependencies for using Spock -->
  <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <version>1.0-groovy-2.4</version>
  </dependency>
  <!-- Optional dependencies for using Spock -->
  <dependency<!-- use a specific Groovy version rather than the one specified by spock-core -->
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.1</version>
  </dependency>
  <dependency<!-- enables mocking of classes (in addition to interfaces) -->
      <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
      <version>3.1</version>
      <!--<scope>profile.test</scope>-->
  </dependency>
  <dependency<!-- enables mocking of classes without default constructor (together with CGLIB) -->
      <groupId>org.objenesis</groupId>
      <artifactId>objenesis</artifactId>
      <version>2.1</version>
      <!--<scope>profile.test</scope>-->
  </dependency>
  <dependency<!-- only required if Hamcrest matchers are used -->
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <!--<scope>profile.test</scope>-->
  </dependency>
  <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.185</version>
  </dependency>
  <dependency>
      <groupId>com.github.rest-driver</groupId>
      <artifactId>rest-server-driver</artifactId>
      <version>1.1.43</version>
  </dependency>
  <dependency<!--mysql 数据库 driver jar-->
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
  </dependency>

<!-- Spring jar-->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.2.3.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.2.3.RELEASE</version>
      <!--<version>${springframework.version}</version>-->
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.3.RELEASE</version>
      <!--<version>${springframework.version}</version>-->
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.3.RELEASE</version>
      <!--<version>${springframework.version}</version>-->
  </dependency>
  <!--<dependency>-->
  <!--<groupId>org.springframework</groupId>-->
  <!--<artifactId>spring-context-support</artifactId>-->
  <!--<!–<version>${springframework.version}</version>–>-->
  <!--</dependency>-->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.2.3.RELEASE</version>
  </dependency>

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.5.4</version>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.4</version>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.5.4</version>
  </dependency>

<!--读取Excel-->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.10-FINAL</version>
  </dependency>

<!--Json-->
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.1.15</version>
  </dependency>

<dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <version>2.2.0</version>
  </dependency>

<!-- guava -->
  <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
  </dependency>
  <!-- Joda Time-->

  <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.3</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/javassist/javassist -->
  <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.12.1.GA</version>
  </dependency>

<!-- // if you don't already have slf4j-api and an implementation of it in the classpath, add this! -->
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.13</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.13</version>
      <scope>test</scope>
  </dependency>
  <!-- https://mvnrepository.com/artifact/junit/junit -->
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.5</version>
  </dependency>
<!--+++++++++++++++++++++++++++++++junit+ant+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
  <!-- https://mvnrepository.com/artifact/org.apache.ant/ant-trax -->
  <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant-trax</artifactId>
      <version>1.8.0</version>
  </dependency>
  <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant-junit</artifactId>
      <version>1.10.1</version>
  </dependency>
  <dependency>
      <groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
      <artifactId>maven-antrun-extended-plugin</artifactId>
      <version>1.43</version>
  </dependency>
  <!--++++++++++++++++++++++++++++++++++++++testng report++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
  <!--<dependency>-->
      <!--<groupId>com.relevantcodes</groupId>-->
      <!--<artifactId>extentreports</artifactId>-->
      <!--<version>2.41.1</version>-->
  <!--</dependency>-->
  <!--<dependency>-->
      <!--<groupId>com.vimalselvam</groupId>-->
      <!--<artifactId>testng-extentsreport</artifactId>-->
      <!--<version>1.3.1</version>-->
  <!--</dependency>-->
  <!--<dependency>-->
      <!--<groupId>com.aventstack</groupId>-->
      <!--<artifactId>extentreports</artifactId>-->
      <!--<version>3.0.6</version>-->
  <!--</dependency>-->
  <!--<!– https://mvnrepository.com/artifact/org.testng/testng –>-->
  <!--<dependency>-->
      <!--<groupId>org.testng</groupId>-->
      <!--<artifactId>testng</artifactId>-->
      <!--<version>6.9.10</version>-->
      <!--<!– <scope>test</scope> –>-->
  <!--</dependency>-->

3、仓库

<repositories>
    <!-- Only required if a snapshot version of Spock is used -->
    <repository>
        <id>spock-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<!-- Only required in spock-example build -->
<distributionManagement>
    <repository>
        <id>foo</id>
        <url>file:///fake.repository.to.make.maven.happy</url>
    </repository>
    <snapshotRepository>
        <id>bar</id>
        <url>file:///fake.repository.to.make.maven.happy</url>
    </snapshotRepository>
</distributionManagement>

4、创建maven项目,目录结构如下:

二、spock框架的脚本开发

1、创建测试类基类,继承spock框架的抽象类Specification,具体用例测试类继承该基类,在基类中可以对数据库,cookie进行初始化。

/**
 * 测试类-基类
 * (添加了几个db连接的static属性)
 *
 * Created by zhf2015 on 16/7/13.
 */
class BaseSpockTest extends Specification {
    @Shared static themis_db = CommonUtil.getSqlInstanceOfthemisdb()

/**  @Shared static cookie =

*   collection.version.Common.getcookie(Common.loginurl,Common.loginparam)

*/
    @Shared header_cookie = RestServerDriver.header("Cookie",cookie)
}

2、创建具体测试类,继承上述基类,以下是最简单的写法

package themis.shenpijieguo

import spock.lang.Unroll
import themis.BaseSpockTest
import themis.Common

class applydetailSearchByReportSpec extends BaseSpockTest {

def setupSpec() {
        //TODO: 设置每个测试类的环境
    }
    def setup() {
        //TODO: 设置每个测试方法的环境,每个测试方法执行一次
    }

@Unroll("该case验证接口url=#url,入参applyStatus=#applyStatus,pageSize=#pageSize,applyId=#applyId,page=#page,ruleId=#ruleId")
    def "测试方法1"() {
        given"清理脏数据"
//        handleOk &= themis_db.execute('''delete from case_operation_records where  id=?''',id);
//        and: "添加测试数据"
//        handleOk &= themis_db.execute('''delete from case_operation_records where  id=?''',id);
        println("themis_db:" themis_db.getConnection().getMetaPropertyValues().toString())
        and"实际值"
        def paramStr=""
        HashMap paramMap = new HashMap();
        paramMap.put("applyStatus",applyStatus)
        paramMap.put("pageSize",pageSize)
        paramMap.put("page",page)
        paramMap.put("ruleId",ruleId)
        paramMap.put("applyId",applyId)

Set keySets=paramMap.keySet()
        for (String keyName:keySets){
            if (paramMap.get(keyName)!=""){
                paramStr+="&"+keyName+"="+paramMap.get(keyName)
            }
        }
        paramStr=paramStr.substring(1, paramStr.length())
        def reponse = Common.PostUrl(url,paramStr, header_cookie)
//        def content = reponse.getContent()
        def jsonNodes=reponse.asJson()
        println("paramStr:" + paramStr)
        println("reponse:" + reponse)
        println("jsonNodes:" + jsonNodes)
        def r_status = reponse.getStatusCode()
        def r_success=jsonNodes.get("success").asText()
        def r_errorMsg=jsonNodes.get("errorMsg").asText()
        def r_errorCode=jsonNodes.get("errorCode").asText()
        def r_attributes=jsonNodes.get("attributes").asText()
        def r_attributesJson=jsonNodes.get("attributesJson").asText()
        def r_exception=jsonNodes.get("exception").asText()
        def r_total=jsonNodes.get("total").asInt()
        def r_hasMore=jsonNodes.get("hasMore").asText()

and"预期值"
        //TODO:
        expect"随处可用的断言"
        r_status==200&&r_success=="true"&&r_errorMsg==""&&r_errorCode==""&&r_attributes=="null"&&r_attributesJson==""&&r_exception=="null"&&r_total==1&&r_hasMore=="false"

        where"不是必需的测试数据"
        index  |   applyId|ruleId|page|pageSize|applyStatus| url
        1      |   10975001|2230012|1|10 |""|Common.hostUrl+"/apply/detailSearchByReport.json"
    }

def cleanup() {
        //TODO: 清理每个测试方法的环境,每个测试方法执行一次
    }

def cleanupSepc() {
        //TODO: 清理每个测试类的环境
    }
}

三、基于spock框架的用例执行并生成HTML报告

1、采用Junit 执行或者直接运行类文件

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import themis.shenpijieguo.applydetailSearchByReportSpec;
import themis.shenpijieguo.applydetailSearchSpec;

@RunWith(Suite.class)
@SuiteClasses({applydetailSearchByReportSpec.class,applydetailSearchSpec.class})
public class JunitTestSuites {
}

2、利用插件中的maven-surefire-plugin+maven-surefire-report-plugin执行生成结果HTML报告:

1)pom.xml文件中maven-surefire-plugin中指定需要执行的测试类

2)使用mvn clean test surefire-report:report命令相当于使用了mvn clean test,mvn surefire-report:report两个命令

3)打开项目路径中\target\site\surefire-report.html文件即为结果报告,报告格式不好,建议使用下面方式执行。

3、利用maven-antrun-extended-plugin插件生成ant 生成的HTML报告

1)在pom.xml中添加maven-surefire-plugin和maven-antrun-extended-plugin

插件

2)使用mvn clean test直接运行。

3)打开/target/surefire-reports/index.html文件,显示结果报告

四、集成jenkins生成HTML报告

1)使用publish Junit test result插件生成Junit HTML报告.构建自由风格的job,适合结果文件为xml格式

Job设置:

结果报告:

2)使用publish HTML report插件生成ant HTML报告,适合结果文件是HTML格式的方式。

I: Job设置

Ii. 查看报告

注:ant生成的HTML报告,使用chrome打开时会出现jenkins的安全策略原因导致无法显示,这时可以用IE去显示。

解决办法参考:

https://zhuanlan.zhihu.com/p/28080975

五、通过tomcat访问HTML报告

1、修改tomcat服务器conf文件夹下的server.xml文件,添加context标签如下所示

2、增加以下标签,其中docBase为需要访问的外部文件夹路径

3、启动tomcat服务器,输入url:http://localhost:8080/index.html, 可以访问HTML静态资源。

Groovy(java)+Spock+IDEA+maven+Jenkins+SVN+maven-surefire-plugin+maven-surefire-report-plugin/maven-antrun-extended-plugin集成接口测试框架的更多相关文章

  1. 搭建基于IDEA+Selenium+Java+TestNG+Maven+Jenkins+SVN的Web端UI自动化测试环境

    第一步:工具下载安装配置 JDK安装与配置 IDEA安装与配置 Maven安装与配置 Tomcat部署与配置 Jenkins部署与配置 Svn安装与配置 各浏览器驱动下载与配置 第二步:集成各个工具到 ...

  2. java+maven+jenkins+svn构建

    操作参照:https://blog.csdn.net/qq_34977342/article/details/82346915 1.创建一个自由风格的项目,起名字 2.设置构建项目最大保存数量,与天数 ...

  3. Selenium+TestNG+Maven+Jenkins+SVN(转载)

    转载自:https://blog.csdn.net/u014202301/article/details/72354069 一. 创建Maven项目,下载Selenium和TestNG的依赖(依赖可以 ...

  4. Jenkins+SVN+Maven+shell 自动化部署实践

      JAVA环境中利用Jenkins+svn+maven进行自动化部署实践   一. 前言2 1.介绍jenkins2 1.本地项目打包2 2.通过secureCRT工具,手动传输到服务器2 3.然后 ...

  5. 自动化测试框架:jmeter + maven+ jenkins

    原理:jenkins驱动maven执行,maven驱动jmeter执行 前提条件:windows安装了jmeter.maven.tomcat.jenkins 安装方法参考汇总目录中对应的博文:http ...

  6. 项目持续集成环境(jenkins + SVN + maven + tomcat)

    整体流程 每次SVN上代码有变动,触发自动构建动作,并部署到服务器的tomcat上,具体流程: 1.SVN上提交代码修改 2.maven执行Goals 3.将web工程打成war包 4.关闭服务器的t ...

  7. 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(二)

    前言     上一篇随笔Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)介绍maven和nexus的环境搭建,以及如何使用maven和nexus统一管理库 ...

  8. Jenkins+svn+maven

    首先我们在我们的服务器上安装好svn和maven 这里在前两步骤基本上没有啥问题,主要就是在Jenkins的步骤我弄了好长时间,这里记录一下 Jenkins的问题我是在这个网址解决的:http://b ...

  9. Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(二)

    上一篇随笔Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)介绍maven和nexus的环境搭建,以及如何使用maven和nexus统一管理库文件和版本,以 ...

随机推荐

  1. oracle 11g安装教程

    oracle 11g安装教程 第1步 第2步 第3步 第4步 第5步 第6步 第7步 第8步 第9步 第10步 第11步 第12步 第13步 第14步 第15步 第16步 第17步 第18步 第19步 ...

  2. 【笔试题】Spring笔试题

    spring笔试题 1.Spring支持的事务管理类型 Spring支持两种类型的事务管理: 编程式事务管理:这意味你通过编程的方式管理事务,给你带来极大的灵活性,但是难维护. 声明式事务管理:这意味 ...

  3. SSH整合错误三连

    访问Action错误 ognl.MethodFailedException: Method "add" failed for object com.test3.action.Use ...

  4. Android Theme.AppCompat.Light的解决方法

    styles.xml中<style name="AppBaseTheme" parent="Theme.AppCompat.Light">提示如下错 ...

  5. Loj#6434「PKUSC2018」主斗地(搜索)

    题面 Loj 题解 细节比较多的搜索题. 首先现将牌型暴力枚举出来,大概是\(3^{16}\)吧. 然后再看能打什么,简化后无非就三种决策:单牌,\(3+x\)和\(4+x\). 枚举网友打了几张\( ...

  6. java 反射 获取Class对象的三种方式

    String str="hello world"; //方式一 :通过对象的getClass()方法 Class<?> clazz1 =str.getClass(); ...

  7. ElasticSearch Mapping中的字段类型

    1)string: 默认会被分词 2)数字类型主要如下几种: long:64位存储  integer:32位存储  short:16位存储  byte:8位存储  double:64位双精度存储  f ...

  8. 初见Python<7>:Python操作mysql

    1.基本介绍: python标准数据库接口为python DB-API,它为开发人员提供了数据库应用编程接口,可以支持mysql.Oracle.MSSQL.Sybase等多种数据库,不同的数据库需要下 ...

  9. [BZOJ4698][SDOI2008]Sandy的卡片(后缀自动机)

    差分之后就是求多串LCS. 对其中一个串建SAM,然后把其它串放在上面跑. 对SAM上的每个状态都用f[x]记录这个状态与当前串的最长匹配长度,res[x]是对每次的f[x]取最小值.答案就是res[ ...

  10. Call to undefined function imageftbbox()

    mac自带的php的验证码出现问题,搜索了一下Call to undefined function imageftbbox(),然后根据这个网站https://php-osx.liip.ch/本剧本机 ...