简体   繁体   English

测试API时如何避免代码重复

[英]How to avoid code duplication when testing API

My question is related to test automation. 我的问题与测试自动化有关。 Moving from postman to intellij with rest-assured, but I'm new to it. 可以放心地从邮递员转到intellij,但我是新手。

Question: What is the best practice to store requests (request bodies?) in test automation projects? 问题:在自动化测试项目中存储请求(请求主体)的最佳实践是什么?

Additional info: Our service returns prices for various main services, additional services, countries and so on. 附加信息:我们的服务返回各种主要服务,附加服务,国家/地区等的价格。 One way - I could store all of the requests separately, but I think it's not good because with time it will become difficult to maintain. 一种方法-我可以将所有请求分开存储,但是我认为这样做不好,因为随着时间的推移,它将很难维护。 Currently I am using postman where I have over 700 requests. 目前,我在使用邮递员,那里有700多个请求。 Most of those requests share at least 60% of the request body. 这些请求中的大多数至少共享请求主体的60%。 I wonder if there is a smart way to create some sort of a template method that I can call whenever I need to create a request and add different input parameters that will have impact on the response. 我想知道是否存在一种聪明的方法来创建某种模板方法,每当我需要创建请求并添加不同的输入参数(会影响响应)时就可以调用该模板方法。

Thank you. 谢谢。

Took me a while to figure this out, but hopefully it will help someone. 花了我一段时间来解决这个问题,但希望它能对某人有所帮助。

To answer the question "what's the best way to store requests (request bodies)..." 回答问题“什么是存储请求(请求主体)的最佳方式...”

depends on the project. 取决于项目。 our developers using data transfer objects (DTO) with builder pattern. 我们的开发人员使用带有构建器模式的数据传输对象(DTO)。 So, I am also using builder pattern. 因此,我也在使用构建器模式。

  1. create a class for request template and fill it with default values (in my example I added jar file with classes from dev's project to my test project so I refer I can refer to them) 为请求模板创建一个类,并用默认值填充它(在我的示例中,我从开发项目的项目向测试项目中添加了带有类的jar文件,因此我可以参考它们)
  2. create a class for request builders 为请求构建器创建一个类
  3. go on with tests! 继续测试!

instead of duplicating request for each test you simply build the request and call builder to override default values for those you need for test. 无需为每个测试重复请求,您只需构建请求并调用构建器即可覆盖测试所需的默认值。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM