SDET Unicorns

API Tests Optimization – Reorganizing Tests

Table of Content

So far in the previous posts, we wrote a few API tests for various HTTP methods and we were relying on the data that already existed on our test site. The challenge with that is if the existing data changes or gets removed it’ll end up breaking our tests. Let’s take a look at how we can fix that.

Current test structure

This is how the tests have been structured so far:

// GET Tests - uses existing userId to get the user data
// POST Test - creates a new user
// PUT Test - uses existing userId to update the user data
// DELETE Test - uses existing userId to delete the user data

So clearly with the DELETE test, we cannot run it multiple times as we are using existing userId and as a result, it would throw a 404 error.

Reorganize tests

So we can fix the above issue by simply reorganizing the way we have written our tests.

// POST Test - creates a new user and stores a new userId
// GET Test - get the new userId from the POST test
// PUT Test - get the new userId to update the user data
// DELETE Test - get the new userId to delete the user data

What we did here is moved our POST test on the top to create a new user and then passed the userId to the rest of the tests. This way despite how many times we run this test file it’ll always work unlike the previous set of tests. ?

Now, I know there’s a downside to this also as all the tests are dependent on the first test but I’d rather prefer this over using existing data that we can’t control. ?‍♂️

Check out this video to see a detailed explanation of how I reorganized the tests:

You can also clone the GitHub repo to access this code


To learn more about API testing, check out my free tutorial series here –

JavaScript API Automation Testing Tutorial Series

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »