rest assured 接口测试作业

这个是社区《20课时全面掌握移动测试必备技能》课程的作业,因为还是花了好几个晚上来做,所以写一个笔记在这里:
一、作业要求
testerhome首页验证,断言精华贴的数量大于1:
https://testerhome.com/api/v3/topics.json

二、方案
TestLink + Jenkins + Maven + Rest Assured + GitHub
计划是在Jenkins上执行,执行结果发邮件到邮箱
本来是准备自己搭建Gitlab的,已经搭建好了,但是虚拟机实在跑不动。。。还是用Github吧。

三、环境准备
1、服务器VMware虚拟机安装Ubuntu16.04LTS
设置固定IP地址:sudo vi vi /etc/network/interfaces,然后用 sudo ifdown 和 sudo ifup 来重启网卡
设置DNS:sudo vi /etc/resolvconf/resolv.conf.d/base,然后 sudo resolvconf -u
安装vsftpd:sudo apt-get install vsftpd,然后修改 /etc/vsftpd.conf 放开权限
安装JDK8:拷贝解压jdk,设置环境变量
安装Git:sudo apt-get install git-core
安装Maven:sudo apt-get install maven

2、安装Jenkins
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

将deb https://pkg.jenkins.io/debian-stable binary/添加到/etc/apt/sources.list

sudo apt-get update
sudo apt-get install jenkins

3、安装testlink
下载地址:http://www.testlink.org/
其实可以使用xmapp(就不用单独安装apache/php/mysql,而且windows下也可以使用)的,非常简单。但是由于我上次已经尝试过了,这次使用复杂方式安装。

安装apache:
sudo apt-get update
sudo apt-get install apache2

安装mysql:
sudo apt-get update
sudo apt-get mysql-server

安装php:
sudo apt-get install -y php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-zip php7.0-curl

安装testlink:
从testlink官网下载testlink-1.9.16.tar.gz,解压后放到apache2服务器的document root下
重启apache2:service apache2 restart
通过浏览器访问apache2,打开testlink的安装页面
1)选择New installation,在检查系统是否满足安装条件时
2)可能需要手工去改大 php.ini 的 session.gc_maxlifetime 和 max_execution_time
3)目录报不存在的,手工创建
4)目录报没有写权限的:sudo chmod 777
5)安装完成后,需要按照提示,创建config_db.inc.php文件

四、代码编写
pom.xml里加入以来如下:

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>
        </dependency>

pom.xml文件中,禁用surefire,使用failsafe,并指定suit文件的位置:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- Disable unit tests -->
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

测试代码:

public class TesertHomeInterfaceTest {
    @Test
    public void test_Excellent_Topics_MoreThan_One(){
        final String uri = "https://testerhome.com/api/v3/topics.json";

        given().when()
                .get(uri)
                .then()
                .body("topics.findAll{it.excellent == 1}.size()", greaterThan(1));
    }
}

上传代码到Github:略,实在没啥好说的。

四、配置
1、配置jenkins
安装插件:Git plugin、GitHub plugin(Git API Plugin)、Maven Integration plugin、TestLink Plugin
配置Git:在 Global Tool Configuration 里,配置 git 的位置,如果 git 安装在系统路径下,也可以直接写git
配置Maven:在 Global Tool Configuration 里,配置MAVEN_HOME(就是maven的安装目录,这个可以通过 mvn --version 看到)
配置JDK:配置jdk的地址(JAVA_HOME)
配置访问Github的用户名和密码:新建一个Credentials,输入Github的用户名和密码
新建任务(这里可以设置为一旦有人修改了代码就执行构建):
1)选择”构建一个maven项目“
2)勾选“github project”并填写自己的测试程序的仓库地址
3)源码管理勾选“Git”,填写仓库地址和选择前面建好的Credentials,根据实际情况填写Branches:*/master // 这里看是master还是dev
4)Build里,输入pom.xml的位置(这里有可能是放在子目录下的),和Goals:clean verify
5)添加一个构建后操作:Editable Email Notification // 用于发构建结果到指定邮箱,这个就略了,太多敏感信息

2、配置testlink
testlink大家都用熟了的,就随便写了
1)新建一个用户、测试项目、测试计划、测试集、测试执行。 // 项目、计划这些别用中文,你懂的
2)新建一个Custom fields:java_class // 名字随意取,其他都不用改动
2)新建测试用例”验证首页精华贴数量大于1",设置用例测试方法为“自动”,设置Custom fields为:
com.xxxx.autotest.TesterHomeInterfaceTest#test_Excellent_Topics_MoreThan_One
这里前面是class名,后面是方法名,对应到上面写的java代码。
3)添加测试用例到测试计划

3、配置jenkins和testlink集成
1)在testlink里,点击账号后的My Settings,产生一个新的API interface key
2)在jenkins里,“系统管理”-“Global Tool Configuration”,输入testlink的信息,包括随便取一个名字,输入xmlrpc.php的地址
这个地址默认是这样的:http://IP:port/lib/api/xmlrpc/v1/xmlrpc.php
然后Developer Key里输入步骤1中产生的key
3)在jenkins任务里,新增一个Post Steps(构建后步骤):Invoke TestLink
4)在Result Seeking Strategy里,新增:TestNG mothod name
Include Pattern:target\surefire-reports\testng-results.xml
Key Custom Field:java_class

4、手工构建检查结果
1)构建后,jenkins这边可以看到测试用例“验证首页精华贴数量大于1”的Execution status为“Passed”

2)构建后,testlink这边可以看到测试执行里,用例被标记为通过