当前位置:   article > 正文

【自动化测试实战】从 0 到 1 搭建 Dubbo 接口自动化测试_dubbo自动化测试

dubbo自动化测试

前言

1、Dubbo 接口自动化测试框架实现逻辑

2、框架具体功能

3、关键实践

3.1 下载 provider 的 jar 包,并代理声明 +zookeeper 设置

3.2 邮件发送功能

3.3 封装 Dubbo 接口信息类

3.4 利用 JMeter 调用 provider 服务,并断言,邮件报警

3.5 利用 Testng 注释,调用 Dubbo 接口类,进行测试

4、利用 Jenkins 自动化运行 Dubbo 测试项目

软件测试面试小程序


前言

由于公司 Dubbo 接口数量较多,且核心接口较多,故需要一套 Dubbo 接口自动化框架,来提高测试效率。

1、Dubbo 接口自动化测试框架实现逻辑

2、框架具体功能

框架需要
实现功能

功能说明当前版本是否已实现从 maven 库自动下载所需 jar 包为了更好的自动化,所有的 provider 的 jar 都从 maven 下载,避免手工导入已实现参数自定义匹配不同的 Dubbo 接口,不同的参数需求已实现断言功能一个接口是否调用成功,在于断言是否成功已实现邮件报警功能如果 Dubbo 接口调用 provider 失败,自动进行邮件报警已实现自动运行利用 jenkins 自动运行已实现

3、关键实践

由于目前阶段刚接触 java 及 Dubbo,本次实现为基本功能实现,数据隔离等没有做。

3.1 下载 provider 的 jar 包,并代理声明 +zookeeper 设置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:Dubbo="http://code.alibabatech.com/schema/Dubbo"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://code.alibabatech.com/schema/Dubbo
  9. http://code.alibabatech.com/schema/Dubbo/Dubbo.xsd">
  10. <Dubbo:application name="demo-consumer"/>
  11. <Dubbo:registry address="zookeeper://1127.0.0.1:2181" />
  12. <Dubbo:reference id="IPromiseForOrderService" interface="com.test.api.IPromiseForOrderService" version="1.0" check="true"/>
  13. </beans>

3.2 邮件发送功能

(1)邮件服务器配置在 mailConfig.properties


(2)获取 mailconfig 信息,并封装成类

  1. public class MailConfig {
  2. private static final String PROPERTIES_DEFAULT = "mailConfig.properties";
  3. public static String host;
  4. public static Integer port;
  5. public static String userName;
  6. public static String passWord;
  7. public static String emailForm;
  8. public static String timeout;
  9. public static String personal;
  10. public static Properties properties;
  11. static{
  12. init();
  13. }
  14. /**
  15. * 初始化
  16. */
  17. private static void init() {
  18. properties = new Properties();
  19. InputStream inputStream = null;
  20. try{
  21. inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
  22. properties.load(inputStream);
  23. inputStream.close();
  24. host = properties.getProperty("mailHost");
  25. port = Integer.parseInt(properties.getProperty("mailPort"));
  26. userName = properties.getProperty("mailUsername");
  27. passWord = properties.getProperty("mailPassword");
  28. emailForm = properties.getProperty("mailFrom");
  29. timeout = properties.getProperty("mailTimeout");
  30. personal = " 自动化测试 ";
  31. } catch(IOException e){
  32. e.printStackTrace();
  33. }
  34. }
  35. }

(3)封装发送邮件功能

  1. public class MailUtil {
  2. private static final String HOST = MailConfig.host;
  3. private static final Integer PORT = MailConfig.port;
  4. private static final String USERNAME = MailConfig.userName;
  5. private static final String PASSWORD = MailConfig.passWord;
  6. private static final String emailForm = MailConfig.emailForm;
  7. private static final String timeout = MailConfig.timeout;
  8. private static final String personal = MailConfig.personal;
  9. private static JavaMailSenderImpl mailSender = createMailSender();
  10. /**
  11. * 邮件发送器
  12. *
  13. * @return 配置好的工具
  14. */
  15. private static JavaMailSenderImpl createMailSender() {
  16. JavaMailSenderImpl sender = new JavaMailSenderImpl();
  17. sender.setHost(HOST);
  18. sender.setPort(PORT);
  19. sender.setUsername(USERNAME);
  20. sender.setPassword(PASSWORD);
  21. sender.setDefaultEncoding("Utf-8");
  22. Properties p = new Properties();
  23. p.setProperty("mail.smtp.timeout", timeout);
  24. p.setProperty("mail.smtp.auth", "false");
  25. sender.setJavaMailProperties(p);
  26. return sender;
  27. }
  28. /**
  29. * 发送邮件
  30. *
  31. * @param to 接受人
  32. * @param subject 主题
  33. * @param html 发送内容
  34. * @throws MessagingException 异常
  35. * @throws UnsupportedEncodingException 异常
  36. */
  37. public void sendMail(InternetAddress[] to, String subject, String html) throws MessagingException,UnsupportedEncodingException {
  38. MimeMessage mimeMessage = mailSender.createMimeMessage();
  39. // 设置 utf-8 或 GBK 编码,否则邮件会有乱码
  40. MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
  41. messageHelper.setFrom(emailForm, personal);
  42. messageHelper.setTo(to);
  43. messageHelper.setSubject(subject);
  44. messageHelper.setText(html, false);
  45. mailSender.send(mimeMessage);
  46. }
  47. }

3.3 封装 Dubbo 接口信息类

把 Dubbo 接口封装成一个类,方便信息 get 和 set

  1. public class DubboInterfaceInfo {
  2. private String DubboInterfaceWiki;
  3. private String DubboInterfacePacketName;
  4. private String DubboInterfaceClassName;
  5. private RestRequest request;
  6. private String responseStatusSuccessful;
  7. private String responseMessageSuccessful;
  8. private String DubboInterfaceId;
  9. public DubboInterfaceInfo() {}
  10. public String getDubboInterfaceWiki() {
  11. return this.DubboInterfaceWiki;
  12. }
  13. public void setDubboInterfaceWiki(String DubboInterfaceWiki) {
  14. this.DubboInterfaceWiki = DubboInterfaceWiki;
  15. }
  16. public String getDubboInterfacePacketName() {
  17. return this.DubboInterfacePacketName;
  18. }
  19. public void setDubboInterfacePacketName(String DubboInterfacePacketName) {
  20. this.DubboInterfacePacketName = DubboInterfacePacketName;
  21. }
  22. public String getDubboInterfaceClassName() {
  23. return this.DubboInterfaceClassName;
  24. }
  25. public void setDubboInterfaceClassName(String DubboInterfaceClassName) {
  26. this.DubboInterfaceClassName = DubboInterfaceClassName;
  27. }
  28. public RestRequest getRestRequest() {
  29. return this.request;
  30. }
  31. public void setRestRequest(RestRequest request) {
  32. this.request = request;
  33. }
  34. public String getResponseStatusSuccessful() {
  35. return this.responseStatusSuccessful;
  36. }
  37. public void setResponseStatusSuccessful(String responseStatusSuccessful) {
  38. this.responseStatusSuccessful = responseStatusSuccessful;
  39. }
  40. public String getResponseMessageSuccessful() {
  41. return this.responseMessageSuccessful;
  42. }
  43. public void setResponseMessageSuccessful(String responseMessageSuccessful) {
  44. this.responseMessageSuccessful = responseMessageSuccessful;
  45. }
  46. public String getDubboInterfaceId() {
  47. return this.DubboInterfaceId;
  48. }
  49. public void setDubboInterfaceId(String DubboInterfaceId) {
  50. this.DubboInterfaceId = DubboInterfaceId;
  51. }
  52. }

3.4 利用 JMeter 调用 provider 服务,并断言,邮件报警

利用 Jmeter 调用 provider 服务,并断言,邮件报警,这些功能封装成 Dubbo 接口测试类,代码如下:

  1. public class IPromiseForOrderServiceTest extends AbstractJavaSamplerClient {
  2. /**
  3. * CONTEXT
  4. * 读取 Dubbo-config.xml 中的内容
  5. */
  6. private static final ApplicationContext CONTEXT = new ClassPathXmlApplicationContext("Dubbo-config.xml");
  7. public DubboInterfaceInfo DubboInterfaceInfo = new DubboInterfaceInfo();
  8. public String responseSuccess;
  9. public String responseFail;
  10. public FreeStockForOrderParam request;
  11. /**
  12. * IPromiseForOrderService
  13. * 此处需要实例化 Dubbo 接口 IPromiseForOrderService,像定义变量一样实例化
  14. */
  15. private static IPromiseForOrderService IPromiseForOrderService;
  16. /**
  17. * IPromiseForOrderService
  18. * 以下方法用于 输入 Dubbo 接口信息
  19. */
  20. public void DubboInterfaceInfoInitialization () {
  21. DubboInterfaceInfo.setDubboInterfaceWiki("......");
  22. DubboInterfaceInfo.setDubboInterfacePacketName("......");
  23. DubboInterfaceInfo.setDubboInterfaceClassName("......");
  24. DubboInterfaceInfo.setDubboInterfaceId("......");
  25. DubboInterfaceInfo.setResponseStatusSuccessful("0");
  26. DubboInterfaceInfo.setResponseMessageSuccessful("success");
  27. String orderNo = "orderNo";
  28. String operater="";
  29. String channel="";
  30. String operateId="operateId";
  31. String version= PromiseVersion.V_1_0_0.getVersion();
  32. request = new FreeStockForOrderParam();
  33. if (orderNo != null || orderNo.length() > 0) {
  34. request.setOrderNo(orderNo);
  35. }
  36. if (operater != null || operater.length() > 0) {
  37. request.setOperater(operater);
  38. }
  39. if (channel != null || channel.length() > 0) {
  40. request.setChannel(channel);
  41. }
  42. if (operateId != null || operateId.length() > 0) {
  43. request.setOperateId(operateId);
  44. }
  45. if (version != null || version.length() > 0) {
  46. request.setVersion(version);
  47. }
  48. RestRequest<FreeStockForOrderParam> req = new RestRequest<FreeStockForOrderParam>();
  49. req.setRequest(request);
  50. DubboInterfaceInfo.setRestRequest(req);
  51. }
  52. @Override
  53. public void setupTest(JavaSamplerContext arg0){
  54. IPromiseForOrderService=(IPromiseForOrderService)CONTEXT.getBean("......");
  55. }
  56. @Override
  57. public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
  58. SampleResult sr = new SampleResult();
  59. try {
  60. sr.sampleStart();
  61. RestResponse responseData = IPromiseForOrderService.freeSaleStock(DubboInterfaceInfo.getRestRequest());
  62. // 自定义 Dubbo 调用成功和失败的邮件正文内容
  63. responseSuccess =
  64. "Dubbo 接口: "
  65. + DubboInterfaceInfo.getDubboInterfaceId() + " 请求成功\r\n"
  66. + "WIKI 地址: " + DubboInterfaceInfo.getDubboInterfaceWiki() + "\r\n"
  67. + "PacketName: " + DubboInterfaceInfo.getDubboInterfacePacketName() + "\r\n"
  68. + "ClassName: " + DubboInterfaceInfo.getDubboInterfaceClassName() + "\r\n"
  69. ;
  70. responseFail =
  71. "Dubbo 接口: " + DubboInterfaceInfo.getDubboInterfaceId() + " 请求失败\r\n"
  72. + "WIKI 地址: " + DubboInterfaceInfo.getDubboInterfaceWiki() + "\r\n"
  73. + "PacketName: " + DubboInterfaceInfo.getDubboInterfacePacketName() + "\r\n"
  74. + "ClassName" + DubboInterfaceInfo.getDubboInterfaceClassName() + "\r\n"
  75. + " 请求参数为:Channel: " + request.getChannel() +
  76. " / operater: " + request.getOperater() +
  77. " / OperateId: " + request.getOperateId() +
  78. " / OrderNo: " + request.getOrderNo() +
  79. " / Version: " + request.getVersion()
  80. + "\r\n"
  81. + " 返回结果为:"
  82. + "ResponseStatus: " + responseData.getStatus()
  83. + " / ResponseMessage: " + responseData.getMessage()
  84. + " / ResponseResult: " + responseData.getResult();
  85. /**
  86. * 邮件定义及发送
  87. */
  88. InternetAddress[] address = new InternetAddress[2];
  89. try {
  90. address[0] = new InternetAddress("lalllalala@qq.com");
  91. address[1] = new InternetAddress("3456789@qq.com");
  92. } catch (AddressException e) {
  93. // TODO Auto-generated catch block
  94. e.printStackTrace();
  95. }
  96. MailUtil mailUtil = new MailUtil();
  97. if ((DubboInterfaceInfo.getResponseStatusSuccessful().equals(responseData.getStatus())) && (DubboInterfaceInfo.getResponseMessageSuccessful().equals(responseData.getMessage()))) {
  98. sr.setSuccessful(true);
  99. sr.setResponseData("responseData: " + responseData, "utf-8");
  100. System.out.println(responseSuccess);
  101. mailUtil.sendMail(address,"Dubbo 接口:" + DubboInterfaceInfo.getDubboInterfaceId() + " 请求成功 ",responseSuccess.toString());
  102. } else {
  103. sr.setSuccessful(false);
  104. System.out.println(responseFail);
  105. mailUtil.sendMail(address,"Dubbo 接口:" + DubboInterfaceInfo.getDubboInterfaceId() + " 请求失败 ",responseFail.toString());
  106. }
  107. sr.sampleEnd();
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. return sr;
  112. }
  113. }

3.5 利用 Testng 注释,调用 Dubbo 接口类,进行测试

  1. public class TestngTest {
  2. @Test()
  3. public void testDubboInterface() {
  4. JavaSamplerContext arg0 = new JavaSamplerContext(new Arguments());
  5. Dubbo 接口测试类 TestForIPromiseForOrderService = new Dubbo 接口测试类 ();
  6. TestForIPromiseForOrderService.DubboInterfaceInfoInitialization();
  7. TestForIPromiseForOrderService.setupTest(arg0);
  8. SampleResult sr = TestForIPromiseForOrderService.runTest(arg0);
  9. }
  10. }

4、利用 Jenkins 自动化运行 Dubbo 测试项目

至此,大功告成,你可以完成 Dubbo 接口自动化测试了。另,接下来可以加入关联、测试数据分离等功能,让框架变得更加易用。

最后: 为了回馈铁杆粉丝们,我给大家整理了完整的软件测试视频学习教程,朋友们如果需要可以自行免费领取 【保证100%免费】

加入我的软件测试交流群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

获取方式 :

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/169611
推荐阅读
相关标签
  

闽ICP备14008679号