DEV Community

Ujjwal Kumar Singh
Ujjwal Kumar Singh

Posted on

Today’s Python Challenge for Testers: Deduplicating API Response Values Before Assertion

Have we ever received an API response with duplicate values and our assertion failed, not because the data was wrong, but because duplicates were messing up our comparison?

Scenario: our API returns a list of user IDs, but some IDs appear more than once. Before we assert, we need a clean, unique list.

Here is the Python code :

response_data = [1, 2, 3, 4, 5, 2, 7, 8, 9]
unique_values = []

for data in response_data:
    if data not in unique_values:
        unique_values.append(data)

print(unique_values)
Enter fullscreen mode Exit fullscreen mode
Output:

[1, 2, 3, 4, 5, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode

Duplicate data is removed. Now our assertion runs against clean data.

Why this matters as a Tester:

Asserting against raw API responses without cleaning the data first is a common source of false failures. Understanding how deduplication works at the code level makes us a better judge of when to clean data and when the duplicate itself is the bug.

Github Repo Link

Top comments (0)