简体   繁体   中英

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.

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. Most of those requests share at least 60% of the request body. 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. 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)
  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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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