news 2026/4/3 3:06:07

如何搭建接口自动化测试框架?

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
如何搭建接口自动化测试框架?

🍅点击文末小卡片,免费获取软件测试全套资料,资料在手,涨薪更快

一、原理及特点

  • 参数放在XML文件中进行管理
  • 用httpClient简单封装一个httpUtils工具类
  • 测试用例管理使用了testNg管理,使用了TestNG参数化测试,通过xml文件来执行case。
  • 测试报告这里用到第三方的包ReportNG 项目组织用Maven

二、准备

  • 使用工具:eclipse,maven
  • 用到的第三方jar包:dom4j、reportng、testng
  • 理解难点:httpUtils和xmlUtil工具类的封装;dom4j使用;CookieStore的应用

三、框架构思

1、项目结构

2、用例执行流程

3、接口调用流程

4、调度脚本流程

四、框架实现

1、输入参数

1.1 参数放在XML文件中进行管理

例:这里测试获取角色的接口输入参数为,page和rows,mapRole.xml内容如下

<?xml version="1.0" encoding="UTF-8"?> <map> <bean beanName="GetRole"> <!--Locator lists --> <locator name="page" value="1"></locator> <locator name="rows" value="10"></locator> </bean> </map>

1.2 封装一个xmlUtil工具类负责读取XML,使用第三方的jar包dom4j

1.2.1 xmlUtil中readXMLDocument方法返回值为HashMap<String, String>

public static HashMap<String, String> readXMLDocument(String beanName,String xmlName){ }

2、返回参数

2.1 创建一个接口返回对象ResponseBean

对象ResponseBean,包括status、statusCode、contentType、body、url、method、cookies

2.2 在工具类中在创建一个ReponseUtil工具类

ReponseUtil工具类负责将请求的返回数据CloseableHttpResponse 转换成ResponseBean

public ResponseBean setResponseBean(CloseableHttpResponse httpResponse) { }

3、测试用例

测试用例管理使用了TestNG管理 ,使用了TestNG参数化测试,通过xml文件来执行case

3.1 测试case脚本

public class GetRoleTest { static CookieStore cookieStore ; static CookieUtil cookieUtil=new CookieUtil() ; CloseableHttpClient client; HttpUtils httpUtils=HttpUtils.getInstance(); @Parameters({ "url", "objBean" ,"statusCode","xmlName"}) @BeforeSuite /* * 登录进入系统获取JSESSIONID放入到CookieStore中 * */ public void TestLoginIn(String url ,String objBean, String statusCode,String xmlName) { Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName); client = HttpClients.createDefault(); CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore); //cookieUtil.printResponse(httpResponse); cookieStore=cookieUtil.setCookieStore(httpResponse); } @Parameters({ "url", "objBean" ,"statusCode","body","xmlName"}) @Test(priority = 2) public void TestGetRole(String url ,String objBean, String statusCode,String body,String xmlName) { Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName); client = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore); ResponseBean rb=new ReponseUtil().setResponseBean(httpResponse); // add Assert Assert.assertEquals("OK", rb.getStatus()); Assert.assertEquals(statusCode, rb.getStatusCode()); Assert.assertEquals(true, rb.getBody().contains(body)); } @AfterSuite public void closeClient(){ try { // 关闭流并释放资源 client.close(); } catch (IOException e) { e.printStackTrace(); } } }

[注] 因为API接口测试时每次都要校验Cookie,所有我们每次都先执行登录操作去获取Cookie

3.2 xml文件的编写

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestGetRole" parallel="classes" thread-count="5"> <parameter name="url" value="/sys/login" /> <parameter name="objBean" value="loginIn" /> <parameter name="status" value="OK" /> <parameter name="statusCode" value="200" /> <parameter name="xmlName" value="mapRole" /> <test name="TestGetRole" preserve-order="true"> <parameter name="url" value="/json/getRoleInfo" /> <parameter name="objBean" value="GetRole" /> <parameter name="status" value="OK" /> <parameter name="statusCode" value="200" /> <parameter name="body" value="roleName" /> <classes> <class name="com.lc.testScript.GetRoleTest"> <methods> <include name="TestGetRole" /> <!--<include name="TestGetRole2" />--> </methods> </class> </classes> </test> </suite>

右键->run as ->TestNG Suite,这个场景的的测试用例就可以运行了

4、测试报告和项目组织

测试报告这里用到第三方的包ReportNG 项目组织用Maven

<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> .......................................... .......................................... .......................................... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <xmlFileName1>TestGetRole.xml</xmlFileName> .................这里写testNG对应的XML名称---------------------- <xmlFileName10>TestGetUser.xml</xmlFileName> </properties> <dependencies> .......................... </dependencies> <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/java/testSuites/${xmlFileName}</suiteXmlFile> .................略............ ..............这里的和properties中的xmlFileName想对应............ <suiteXmlFile>src/test/java/testSuites/${xmlFileName10}</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <!-- 添加插件,添加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</value> </property> </properties> <workingDirectory>target/</workingDirectory> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

[注] 因为是maven的项目所以要将testSuite的xml文件放在maven的test目录下,这样右键pom.xml文件maven test,所有的测试用例就开始执行了

测试报告

框架目前存在的不足。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/25 9:54:25

Selenium操作指南

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 大家好&#xff0c;今天带大家一起系统的学习下模拟浏览器运行库Selenium&#xff0c;它是一个用于Web自动化测试及爬虫应用的重要工具。 Selenium测试直接运行在…

作者头像 李华
网站建设 2026/3/28 12:05:12

(附源码)springboot期末考试智能安排系统-计算机毕设 36926

springboot期末考试智能安排系统 摘要 本论文介绍了一种基于Spring Boot框架的期末考试智能安排系统。该系统为不同用户角色提供了定制化的功能模块。学生用户能够访问首页、接收通知公告、获取考试资讯、查询课程信息、了解考场安排、查阅专业信息&#xff0c;并在个人中心查看…

作者头像 李华
网站建设 2026/3/31 11:04:48

腾讯云国际站代理商CSS的服务质量如何保证稳定性?

腾讯云国际站代理商的 CSS 服务依托腾讯云原厂的技术架构基础&#xff0c;搭配代理商自身的专属服务机制&#xff0c;从基建、资源、监控、运维等多个关键环节构建稳定性保障体系&#xff0c;具体措施如下&#xff1a;全球高可用基建打底&#xff0c;筑牢物理与网络层面稳定性代…

作者头像 李华
网站建设 2026/3/26 9:59:21

PDFMathTranslate中文乱码终极修复指南:三步搞定排版错乱

PDFMathTranslate中文乱码终极修复指南&#xff1a;三步搞定排版错乱 【免费下载链接】PDFMathTranslate PDF scientific paper translation with preserved formats - 基于 AI 完整保留排版的 PDF 文档全文双语翻译&#xff0c;支持 Google/DeepL/Ollama/OpenAI 等服务&#x…

作者头像 李华
网站建设 2026/3/29 17:35:39

2025年自主智能体(Agent)架构深度研究报告

摘要2025年标志着人工智能从生成式内容的“副驾驶”&#xff08;Copilot&#xff09;时代&#xff0c;正式迈向自主执行任务的“智能体”&#xff08;Agent&#xff09;时代的转折点。尽管大语言模型&#xff08;LLM&#xff09;的普及让“Agent”这一概念不再稀奇&#xff0c;…

作者头像 李华