【自动测试专题】Rest Assured+TestNg实现数据驱动的接口测试

引言

  笔者之前一直使用Jmeter接口测试,也围绕Jmeter做了一些功能集成,比如:生成excle结果文件、数据库断言、自动提交缺陷、自动更新案例执行结果至Testlink等。虽说Jmeter简单易上手,但大批量执行测试案例时,响应时间较长,这对向来追求测试效率的笔者而言,无疑是心头之痛。

  很早就听说过Rest Assured,TestNg两大框架,也看过一些相关的文章,但苦于各种原因,一直都是浅尝辄止。这两天心血来潮,尝试使用Rest Assured+TestNg来实现数据驱动的接口测试,谁知不“尝(试)”则已,一“尝”惊人,实在是接口测试人员的福音。

  框架介绍

  Rest Assured

  REST Assured是一个可以简化HTTP Builder顶层,基于REST服务的测试过程的Java DSL(针对某一领域,具有受限表达性的一种计算机程序设计语言)。它支持发起POST,GET,PUT,DELETE,OPTIONS,PATCH和HEAD请求,并且可以用来验证和校对这些请求的响应信息。

TestNg

TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).

ReportNg

 ReportNG is a simple HTML reporting plug-in for the TestNG unit-testing framework.

实现功能

  读取excel测试案例数据。

  发送请求。

  断言。

  生成测试报告。

  实现步骤

  1、代码结构及案例模板

代码结构

 

【全网最全】TestNG自动化测试框架入门到实战 

 

【全网最全】TestNG自动化测试框架入门到实战 

案例模板(部分字段预留后续使用)

  2、新建maven项目并配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.test.restassured</groupId>
  <artifactId>restassured</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
  <dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>3.1.0</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.11</version>
  </dependency>
  <dependency>
  <groupId>net.sourceforge.jexcelapi</groupId>
  <artifactId>jxl</artifactId>
  <version>2.6.12</version>
  </dependency>
  <!-- 依赖reportNg 关联testNg-->
  <dependency>
  <groupId>org.uncommons</groupId>
  <artifactId>reportng</artifactId>
  <version>1.1.4</version>
  <scope>test</scope>
  <exclusions>
  <exclusion>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  </exclusion>
  </exclusions>
  </dependency>
  <!-- 依赖Guice -->
  <dependency>
  <groupId>com.google.inject</groupId>
  <artifactId>guice</artifactId>
  <version>4.0</version>
  </dependency>
  </dependencies>
  <build>
  <plugins>
  <!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
  <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.5</version>
  <configuration>
  <properties>
  <property>
  <name>usedefaultlisteners</name>
  <value>false</value>
  </property>
  <property>
  <name>listener</name>
  <value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter</value>
  </property>
  </properties>
  <workingDirectory>target/</workingDirectory>
  <forkMode>always</forkMode>
  </configuration>
  </plugin>
  </plugins>
  </build>
  </project>

 

3、配置ReportNg监听

reportNg监听

  4、读取案例数据

  由于TestNg的@DataProvider注释返回的是二维数组,所以需读取excel案例数据保存到一个二维数组。

public class ReadExcelCases {
  public static Object[][] readCases(String filePath) throws IOException, BiffException {
  InputStream inputStream = new FileInputStream(filePath);
  Workbook rwb = Workbook.getWorkbook(inputStream);
  Sheet sheet = rwb.getSheet(0);
  int rsRows = sheet.getRows(); // 获取总行数
  int rsColums = sheet.getColumns();//获取总列数
  int countY = 0;
  for (int i = 1; i < rsRows; i++) {
  if(sheet.getCell(3, i).getContents().equals("Y"))  //统计需要执行的案例数
  countY++;
  }
  Object[][] cases = new Object[countY][rsColums];
  int x =0;
  for (int i = 1; i < rsRows; i++) {
  if(sheet.getCell(3, i).getContents().equals("Y")){  //执行标识为“Y”才记录数组
  for (int j = 0; j < rsColums; j++) {
  cases[x][j] = sheet.getCell(j, i).getContents();
  }
  x++;
  }
  }
  return cases;
  }
  }
  TestNg的@Test传参有多种方法,具体可百度,本例子使用@DataProvider来传参。
  public class CasesDataProvider {
  @DataProvider(name = "casesProvider")
  public static Object[][] caseProvider() throws IOException, BiffException {
  String filePath = ".\\src\\test\\testCases\\发送短信.xls"; //测试案例相对路径
  Object[][] cases = ReadExcelCases.readCases(filePath);
  return cases;
  }
  }

 5、执行案例

public class RunTest {
  @BeforeClass
  public void setUp() {
  RestAssured.baseURI = "http://XX.XXX.XXX.XXX";  //请求IP
  RestAssured.basePath = "v1/gateway.do";
  RestAssured.port = 8187;
  }
  @Test(dataProvider = "casesProvider", dataProviderClass = CasesDataProvider.class)
  public void runCases(String caseNo, String testPoit, String preResult, String YorN, String tableCheck, String appId, String merchantId, String api, String version,
  String phone, String bizTransaction, String acctType) {
  String bodyString = "{\n" +
  "\t\"appId\":\"" + appId + "\",\n" +
  "\t\"api\":\"" + api + "\",\n" +
  "\t\"data\":{\n" +
  "\t\t\"merchantId\":\"" + merchantId + "\",\n" +
  "\t\t\"bizTransaction\":\"" + bizTransaction + "\",\n" +
  "\t\t\"phone\":\"" + phone + "\",\n" +
  "\t\t\"acctType\":\"" + acctType + "\"\n" +
  "\t\t},\n" +
  "\t\"version\":\"" + version + "\"\n" +
  "}\n";
  Response response = given()
  .contentType("application/json;charset=UTF-8")
  .request()
  .body(bodyString)
  .post();
  response.prettyPrint();//格式化响应报文
  //断言
  String json = response.asString();
  JsonPath jp = new JsonPath(json);
  if(response.statusCode() == 200){ //请求成功
  Assert.assertEquals(jp.get("message").toString(),preResult);
  }else{
  Assert.assertEquals(jp.get("data.errMsg").toString(),preResult);
  }
  }
  }

6、测试报告

  当然,ReportNg测试报告支持自定义,百度还是好多资源的,后续笔者再做探究。

测试报告

展望

  以上只是Rest Assured+TestNg强大功能的冰山一角,后续笔者再慢慢摸索。另外,既然迈出了这一步,那怎么也得展望一下未来,笔者打算后续搞个接口测试平台玩玩

 【全网最全】TestNG自动化测试框架入门到实战

 

 

【全网最全】TestNG自动化测试框架入门到实战 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我知道了。下面是搭建接口自动化测试框架的步骤: 1. 安装 Eclipse 首先,你需要安装 Eclipse,可以在官网上下载最新版本的 Eclipse IDE。 2. 安装 TestNG 插件 打开 Eclipse 后,进入 Help -> Eclipse Marketplace,在搜索框中输入 TestNG,然后点击 Install 安装 TestNG 插件。 3. 创建 Maven 项目 在 Eclipse 中创建一个 Maven 项目,选择 Create a simple project,然后勾选 Create a simple project (skip archetype selection),接下来输入项目名称和项目路径,最后点击 Finish。 4. 配置 pom.xml 文件 在项目中的 pom.xml 文件中添加以下依赖: ``` <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.4.0</version> </dependency> </dependencies> ``` 这里我们使用了 TestNGRest Assured 两个依赖。 5. 创建测试类 在 src/test/java 目录下创建一个测试类,例如 TestDemo。在测试类中编写测试方法,例如: ``` import org.testng.annotations.Test; import io.restassured.RestAssured; import static io.restassured.RestAssured.*; public class TestDemo { @Test public void testDemo() { RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; given().log().all() .when().get("/posts/1") .then().log().all().statusCode(200); } } ``` 这里的测试方法使用了 Rest Assured 库来发送 HTTP 请求,并使用 TestNG 的断言来验证响应状态码是否为 200。 6. 运行测试 在 Eclipse 中右键点击测试类或测试方法,选择 Run As -> TestNG Test 来运行测试。 这就是使用 Eclipse 和 TestNG 搭建接口自动化测试框架的步骤。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

传说三哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值