• 接口自动化测试框架搭建【附详细搭建视频】


    如果遇到什么问题建议观看下面视频:

    【敢称全站第一】B站最全的Python自动化测试深度学习教程!学完即就业,小白也能信手拈来!帮你少走99%的弯路~

    一、原理及特点
      参数放在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内容如下 

    1.  <?xml version="1.0" encoding="UTF-8"?>
    2.   <map>
    3.   <bean beanName="GetRole">
    4.   <!--Locator lists -->
    5.   <locator name="page" value="1"></locator>
    6.   <locator name="rows" value="10"></locator>
    7.   </bean>
    8.   </map>
    9.   1.2 封装一个xmlUtil工具类负责读取XML,使用第三方的jar包dom4j
    10.   1.2.1 xmlUtil中readXMLDocument方法返回值为HashMap<String, String>
    11.   public static HashMap<String, String> readXMLDocument(String beanName,String xmlName){
    12.   }
    13.   参数xmlName(xml文件的名字); 参数beanName(xml文件中节点的名称);
    14.   1.3 封装一个CookieUtil工具类,通过CookieStore储存cookie
    15.   1.3.1 CookieUtil类中setCookieStore方法返回值为CookieStore
    16.   public CookieStore setCookieStore(HttpResponse httpResponse) {
    17.   }
    18.   1.4 用httpClient简单封装一个httpUtils工具类有get.post,put,delete方法
    19.   1.4.1 httpUtils中post封装方法如下:
    20.   public CloseableHttpResponse post(String url, Map<String, String> params,CloseableHttpClient httpclient,CookieStore cookieStore){
    21.   }

    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脚本

    1. public class GetRoleTest {
    2. static CookieStore cookieStore ;
    3. static CookieUtil cookieUtil=new CookieUtil() ;
    4. CloseableHttpClient client;
    5. HttpUtils httpUtils=HttpUtils.getInstance();
    6. @Parameters({ "url", "objBean" ,"statusCode","xmlName"})
    7. @BeforeSuite
    8. /*
    9. * 登录进入系统获取JSESSIONID放入到CookieStore中
    10. * */
    11. public void TestLoginIn(String url ,String objBean, String statusCode,String xmlName) {
    12. Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName);
    13. client = HttpClients.createDefault();
    14. CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
    15. //cookieUtil.printResponse(httpResponse);
    16. cookieStore=cookieUtil.setCookieStore(httpResponse);
    17. }
    18. @Parameters({ "url", "objBean" ,"statusCode","body","xmlName"})
    19. @Test(priority = 2)
    20. public void TestGetRole(String url ,String objBean, String statusCode,String body,String xmlName) {
    21. Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName);
    22. client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    23. CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
    24. ResponseBean rb=new ReponseUtil().setResponseBean(httpResponse);
    25. // add Assert
    26. Assert.assertEquals("OK", rb.getStatus());
    27. Assert.assertEquals(statusCode, rb.getStatusCode());
    28. Assert.assertEquals(true, rb.getBody().contains(body));
    29. }
    30. @AfterSuite
    31. public void closeClient(){
    32. try {
    33. // 关闭流并释放资源
    34. client.close();
    35. } catch (IOException e) {
    36. e.printStackTrace();
    37. }
    38. }
    39. }

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

    3.2 xml文件的编写

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    3. <suite name="TestGetRole" parallel="classes" thread-count="5">
    4. <parameter name="url" value="/sys/login" />
    5. <parameter name="objBean" value="loginIn" />
    6. <parameter name="status" value="OK" />
    7. <parameter name="statusCode" value="200" />
    8. <parameter name="xmlName" value="mapRole" />
    9. <test name="TestGetRole" preserve-order="true">
    10. <parameter name="url" value="/json/getRoleInfo" />
    11. <parameter name="objBean" value="GetRole" />
    12. <parameter name="status" value="OK" />
    13. <parameter name="statusCode" value="200" />
    14. <parameter name="body" value="roleName" />
    15. <classes>
    16. <class name="com.lc.testScript.GetRoleTest">
    17. <methods>
    18. <include name="TestGetRole" />
    19. <!--<include name="TestGetRole2" />-->
    20. </methods>
    21. </class>
    22. </classes>
    23. </test>
    24. </suite>

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

    4、测试报告和项目组织

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

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. ..........................................
    5. ..........................................
    6. ..........................................
    7. <properties>
    8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    9. <xmlFileName1>TestGetRole.xml</xmlFileName>
    10. .................这里写testNG对应的XML名称----------------------
    11. <xmlFileName10>TestGetUser.xml</xmlFileName>
    12. </properties>
    13. <dependencies>
    14. ..........................
    15. </dependencies>
    16. <build>
    17. <plugin>
    18. <groupId>org.apache.maven.plugins</groupId>
    19. <artifactId>maven-surefire-plugin</artifactId>
    20. <version>2.19</version>
    21. <configuration>
    22. <suiteXmlFiles>
    23. <suiteXmlFile>src/test/java/testSuites/${xmlFileName}</suiteXmlFile>
    24. .................略............
    25. ..............这里的和properties中的xmlFileName想对应............
    26. <suiteXmlFile>src/test/java/testSuites/${xmlFileName10}</suiteXmlFile>
    27. </suiteXmlFiles>
    28. </configuration>
    29. </plugin>
    30. <!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
    31. <plugin>
    32. <groupId>org.apache.maven.plugins</groupId>
    33. <artifactId>maven-surefire-plugin</artifactId>
    34. <version>2.5</version>
    35. <configuration>
    36. <properties>
    37. <property>
    38. <name>usedefaultlisteners</name>
    39. <value>false</value>
    40. </property>
    41. <property>
    42. <name>listener</name>
    43. <value>org.uncommons.reportng.HTMLReporter</value>
    44. </property>
    45. </properties>
    46. <workingDirectory>target/</workingDirectory>
    47. </configuration>
    48. </plugin>
    49. <plugin>
    50. <artifactId>maven-compiler-plugin</artifactId>
    51. <version>3.5.1</version>
    52. <configuration>
    53. <source>1.8</source>
    54. <target>1.8</target>
    55. </configuration>
    56. </plugin>
    57. </plugins>
    58. </build>
    59. </project>

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

    测试报告

     框架目前存在的不足

      1、数据库数据校验这一块的功能还没有完善,计划用MyBatis

      2、参数使用了xml文件配置虽然灵活但有些繁琐,目前还没想到好的解决方案,testlink是否可以尝试一下呢

    最后允许我对你们说一段话:

    如果你也在往自动化测试开发方向发展

    在适当的年龄,选择适当的岗位,尽量去发挥好自己的优势

    我的自动化测试之路,一路走来都离不每个阶段的计划,因为自己喜欢规划和总结,所以,我和朋友特意花了一段时间整理编写了以上的《自动化测试工程师进阶路线》,也整理了不少【网盘资源】,需要的朋友可以扫描下方小卡片获取网盘链接。希望会给你带来帮助和方向。

  • 相关阅读:
    (热门推荐)天津web前端培训班 Web前端学习顺序
    本地连接服务器mysql数据库慢
    15、Mybatis获取参数值的情况1(mapper接口方法的参数为单个字面量类型)
    Unity URP 渲染管线:URP渲染管线光照机制剖析
    SQL Server教程 - T-SQL-DQL(Data Query Language)
    Requests库POST请求
    Linux/Ubuntu/Debian 常用用户管理命令
    60 - 数组类模板
    这么写错误的原因是什么?
    实操演练 | 批量插入的三种方式
  • 原文地址:https://blog.csdn.net/2201_76100073/article/details/132878229