Why your test should fail first
I got the idea for an article about this topic a while ago. At the end of the Technical Testing Fundamentals course, that is part of the program our young graduates follow, I asked for some key takeaways. Although it is a 4 hour course and we handle a lot of different topics, this one resonated with me: “See your test fail first”. It is something we mention a few times and in different contexts, but is never a main topic of the course.
So what do we mean when we say that you should have a failing test first?
The main goal of writing tests is to make sure our application is doing what we want it to do. We also use those same test to verify that we are not breaking anything at a later stage. This ofcourse only works if the tests are actually testing something useful. There are a lot of examples where the test seems to be doing something valuable. But when you take a closer look it is actually just asserting against a mocked or fixed value.
The solution? Make sure you see your test fail first before you do the implementation. This will automatically improve the quality of your tests. First of all we are sure that our test can fail, and more importantly we can verify that it fails for the right reasons. Then we can adapt our code to have this test succeed. This guarantees us that our change was responsible for making the test succeed.
领英推荐
While this doesn’t go as far as Test Driven Design yet, it is already a step in the right direction. This is only a small effort for a developer to do, and will have a great impact on the quality of your tests.
Even if you decide you still want to write the implementation before the test, I think you should still put in some effort to validate your test. You can do this by breaking the implementation or returning a default value. it is just another way of verifying that your test will fail when the implementation breaks.
All of this will improve the confidence in the automated test, making sure you can focus on delivering new value as fast as possible.
Written by Arne Van de Bemdt
?? I.V. Psychotherapist ?? Photographer ??Entrepreneur
1 年Arne Van de Bemdt, Thanks for the article. I guess every time code is adjusted based on test results, it becomes kind of resistent to the test. Therefore checking wether your test cases are still sufficient to detect bugs in the improved code is a must.