DEV Community

Cover image for Writing Resilient Unit Tests

Posted on

Writing Resilient Unit Tests

In the world of software development, unit testing is a critical aspect that ensures the correct behavior of our applications. However, a common pitfall that many developers fall into is creating brittle tests - tests that fail due to unrelated changes in the implementation. This can lead to a lot of unnecessary debugging and frustration. But what if there was a way to design our tests to be resilient to these changes?

In a recent YouTube video, Zoran Horvat, an experienced software developer and educator, sheds light on this very issue. The video, titled "Write Unit Tests Against the Interface, Not Implementation," is a must-watch for any developer looking to improve their unit testing skills.

The Problem with Brittle Tests

Zoran begins by explaining the concept of brittleness in unit tests. A brittle test is one that fails either to compile or run when an unrelated detail in the underlying implementation changes. This is often due to the test being overly reliant on specific implementation details, resulting in a test that is tightly bound to the implementation.

The Resilient Test Approach

The video then transitions into a demonstration of a resilient test. Unlike brittle tests, resilient tests are not bound to the implementation but only to the interface. This approach allows the test to remain valid even when the implementation changes, thus making it resilient to changes in the underlying implementation.

A Real-World Scenario

To illustrate this concept, Zoran uses a real-world scenario from a monetary application. He shows how a brittle test can fail with the slightest shift in implementation, and then contrasts this with a resilient test that remains valid despite significant changes in the implementation.

The Takeaway

The main takeaway from Zoran's video is the importance of designing our tests against the interface and not the implementation. If we do so, we can create tests that are resilient to changes in the underlying implementation, saving us time and frustration in the long run.

Engage with the Video

I highly recommend watching Zoran's video to gain a deeper understanding of this concept. Not only does he explain the theory behind resilient tests, but he also provides a practical demonstration that clearly illustrates the difference between brittle and resilient tests.

So, why not take a moment to watch the video? And while you're at it, don't forget to like, comment, and share the video. Engaging with the video not only helps Zoran but also helps you by increasing the visibility of this valuable content.

Remember, the more we engage, the more we learn. So, let's start a conversation about resilient tests and help each other become better developers.

Watch the video here

Happy testing!

Top comments (1)

sid262000 profile image

" the more we engage, the more we learn"
Loved this line! It's really important to realise the same