Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31
 
 
 

Code Smell 272 - API Chain

DATE POSTED:September 30, 2024

Streamline Your API Tests: Less is More

TL;DR: Use primitive steps to verify API behavior instead of direct requests.

Problems
  • Unnecessary API calls
  • Slow test performance
  • Overcomplicated validations
  • Fragile tests
  • Slow feedback loops
  • Maintainability
  • Misleading test results
Solutions
  1. Test primitive outcomes
  2. Validate responses directly
  3. Avoid extra API steps
  4. Simplify test logic
Context

When you test an API, you might fall into the trap of using multiple API requests to verify a single operation.

\ Making a POST and immediately making a GET to check the existence of the resource with the payload is a straightforward solution.

\ This pattern adds complexity and makes your tests slower.

\ Also, if your GET operation breaks, you will have many tests failing not relating to the test GET operation.

\ The epitome of testing is a single broken test for a wrong behavior.

\ Instead of verifying resource creation through a GET and inspecting the JSON response, you can focus on primitive steps.

\ Check if the POST succeeded by validating the status code or checking the resource's existence.

Sample Code Wrong Feature: Movie Management Scenario: Create a movie and verify When I send a POST request to "/movies" with the following data: | title | director | year | | Klendathu | Christopher Nolan | 2010 | When I send a GET request to "/movies/Klendathu" Then the response status should be 200 And the response should contain: | title | director | year | | Klendathu | Christopher Nolan | 2010 | Right Feature: Movie Management Scenario: Create a movie and verify When I create a movie with the following details: | title | director | year | | Klendathu | Christopher Nolan | 2010 | Then the movie "Klendathu" should exist in the system ## This is a low level existance postcondition ## Without relying on a GET request Detection
  • [x] Semi-Automatic

You can detect this smell when you see test steps that use a GET request to verify the success of a POST.

Tags
  • Testing
Level
  • [x] Intermediate
AI Generation

AI generators often create this smell when generating API tests, defaulting to chaining requests and validating the entire resource, rather than focusing on the operation's outcome.

AI Detection

AI tools can potentially detect this smell with proper instructions.

\ You could train an AI to identify patterns of consecutive POST and GET requests in scenarios and suggest consolidating them into more abstract, primitive steps.

Try Them!

Remember: AI Assistants make lots of mistakes

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini |

Conclusion

Focus your acceptance tests on the direct results of operations like POST.

\ Avoid making a GET request afterward to verify what you already know.

Relations

https://hackernoon.com/code-smell-259-control-your-environment-to-avoid-test-failures?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vi-cmj31om?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xi-sit35t1

More Info

https://hackernoon.com/fail-fast-philosophy-explained-si963vk9?embedable=true

Disclaimer

Code Smells are my opinion.

Credits

Photo by Dmitriy Demidov on Unsplash

As a rule, software systems do not work well until they have been used, and have failed repeatedly, in real applications.

Dave Parnas

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true

This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code