3 Hacks to Crush Test Flakiness

Omri Bashan
3 min readAug 17, 2023

Hey there tech guys! 💻 You know that automated tests are the heroes of software development, right? But let’s be real — dealing with flaky tests can be like challenging. One day they pass, the next they fail, and you’re left scratching your head. Fear not! We will touch three awesome strategies to help you sniff out and squash those flaky test. So, grab your detective hat and let’s roll!

test matrix from buildingbetterteams.

1. Logging Like a Pro 👑

Imagine this: your test suite is a journal, and every test is an adventure. Start by jotting down everything that happens in there:

- Setting the Scene: Scribble down the environment settings, test data, and any friends (read: external stuff) your test hangs out with. Changes in these buddies could be the reason behind flakiness.

- Failures Unveiled: When a test trip over its shoelaces, note where it stumbled, what weird messages it blurted out, and the trail it left (aka the stack trace). Armed with this info, you’re like a test detective, tracking down the mystery glitch.

- Retry Chronicles: If your test system retries, keep tabs on each retry’s saga. This helps you see if a test’s flakiness is consistent or just a mood swing.

2. The Flakiness Fortress 🏯

Imagine you’re building a magical fort to fend off test flakiness creatures. Here’s how:

- Mocking Magic: Swap out real-world friends (dependencies) with pretend pals like mock objects. They mimic the real buddies, so network glitches or moody third-party services won’t mess with your tests.

- Parallel Party: Parallel tests sound cool, but they can be like trying to juggle too many balls. Don’t let tests collide — use tricks like database sharding or thread-safe elements to keep things smooth.

- Database Date Night: If your tests date databases, make sure the dates go well. Reset the database to a clean state before each test or use database magic to undo changes after each run.

3. Be the Test Watcher 💂

Time to put on your test guardian cape:

- Dashboard Delight: Create a dashboard that records test histories. It’s like your test’s Instagram — helps you spot tests that love to show off their flakiness.

- Flaky Metrics: Get all Sherlock Holmes with flakiness. Calculate how many times tests fail unpredictably. Keep an eye on these metrics to catch sneaky patterns.

- Regression Rescue: Have a squad that tests the testers. Whenever new code or tests appear, make them dance through a special test obstacle course to uncover any hidden flakiness.

Conclusion !

Flaky tests don’t stand a chance against your new detective skills! With solid documentation, smart strategies, and vigilance, you’re on your way to taming those wild testing beasts. Your testing suite will become a symphony of reliability, giving you and your team the confidence to rock out in the coding world. So, keep your hat on, keep those pens scribbling, and never stop exploring the labyrinth of test flakiness — the treasure of rock-solid tests awaits!

--

--

Omri Bashan

a 25-years-old automation and quality assurance engineer, with seven years of experience in the industry including 2 as a tech lead.