接口测试 rest assured 接口测试作业

白虹李李 · 2017年11月24日 · 1040 次阅读

这个是社区《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 这边可以看到测试执行里,用例被标记为通过

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册