<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk" -->

---
title: Flaky Test Files: State Pollution | daily.dev
description: State pollution is illustrated as a common cause of flaky API tests: a test that creates a resource with a hardcoded ID (like a book) passes on the first run...
canonical: https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Flaky Test Files: State Pollution | daily.dev
og:description: State pollution is illustrated as a common cause of flaky API tests: a test that creates a resource with a hardcoded ID (like a book) passes on the first run...
og:url: https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk
og:image: https://api.daily.dev/og/posts/hQyCq4AYk.png
og:image:alt: Flaky Test Files: State Pollution
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Flaky Test Files: State Pollution

**[Gil Zilberfeld - Everyday Unit Testing](https://daily.dev/sources/testingil)** · 4 min read · 0 upvotes · 0 comments

## Summary

State pollution is illustrated as a common cause of flaky API tests: a test that creates a resource with a hardcoded ID (like a book) passes on the first run but fails on subsequent runs because the created data was never cleaned up, so the assumed 'always the same starting state' assumption breaks. The fix is to clean up test data or use unique IDs each run, but the deeper lesson is to question whether tests always assume a fresh starting state. Ends with a promotion for a paid API testing masterclass covering flakiness in depth.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://testingil.com/2026/04/the-flaky-test-files-the-case-of-the-state-pollution.html>

## Questions this post answers

### why does my api test pass the first time but fail when I run it again

The most common cause is state pollution: a prior test run created data (like a resource with a fixed ID) that was never cleaned up, so the second run's create call fails because that ID already exists. This happens when tests assume the system always starts from the same clean state without verifying it. Fixing it means deleting created data after the test or generating a unique identifier on every run.

_Debugging repeat-run test failures gets easier when developers compare fixes for flaky test patterns on daily.dev._

## Similar posts on daily.dev

- [The Race Condition Hiding in Your Flaky Test](https://daily.dev/posts/the-race-condition-hiding-in-your-flaky-test-l61f8j8cx) · Mabl Engineering Blog · 0 upvotes · 0 comments
- [Why flaky tests are increasing, and what you can do about it](https://daily.dev/posts/why-flaky-tests-are-increasing-and-what-you-can-do-about-it-vnrza0zoj) · SD Times · 0 upvotes · 0 comments
- [Eliminating Flaky Tests to End World Hunger](https://daily.dev/posts/eliminating-flaky-tests-to-end-world-hunger-28jqhtfjh) · Foojay.io · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#testing](https://daily.dev/tags/testing)

[View this post on daily.dev](https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Flaky Test Files: State Pollution","url":"https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk"},"datePublished":"2026-09-06T08:56:37.171Z","dateModified":"2026-09-06T13:07:21.577Z","description":"State pollution is illustrated as a common cause of flaky API tests: a test that creates a resource with a hardcoded ID (like a book) passes on the first run...","image":"https://i0.wp.com/testingil.com/wp-content/uploads/2026/05/cropped-Testingil-logo-2026.png?fit=760%2C558\u0026ssl=1","thumbnailUrl":"https://i0.wp.com/testingil.com/wp-content/uploads/2026/05/cropped-Testingil-logo-2026.png?fit=760%2C558\u0026ssl=1","isAccessibleForFree":true,"articleSection":"Gil Zilberfeld - Everyday Unit Testing","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Gil Zilberfeld - Everyday Unit Testing","logo":"https://media.daily.dev/image/upload/s--mBrZysP---/f_auto,q_auto/v1788684993/logos/testingil?_a=BAMAMicg0","url":"https://daily.dev/sources/testingil"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,testing","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Gil Zilberfeld - Everyday Unit Testing","item":"https://daily.dev/sources/testingil"},{"@type":"ListItem","position":3,"name":"Flaky Test Files: State Pollution"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/flaky-test-files-state-pollution-hqycq4ayk#faq","mainEntity":[{"@type":"Question","name":"why does my api test pass the first time but fail when I run it again","acceptedAnswer":{"@type":"Answer","text":"The most common cause is state pollution: a prior test run created data (like a resource with a fixed ID) that was never cleaned up, so the second run's create call fails because that ID already exists. This happens when tests assume the system always starts from the same clean state without verifying it. Fixing it means deleting created data after the test or generating a unique identifier on every run. Debugging repeat-run test failures gets easier when developers compare fixes for flaky test patterns on daily.dev."}}]}
```

