DEV Community

dvallin
dvallin

Posted on

Little red failing test and other fables

Instead of writing about my blogging habits during quarantine, I wrote some short fables about unit test methodology for you to read to your kids and/or cats.

The Hare and the Hedgehog

One Sunday morning about harvest time, just as the buckwheat was in bloom, the sun was shining brightly in heaven, the east wind was blowing warmly over the stupple-fields, the larks were singing in the air, the bees buzzing among the buckwheat, the people were all going in their Sunday clothes to church, and all creatures were happy, and the Hedgehog was happy too.

The Hedgehog took a walk in the field, to visit his databases and look after his unit tests. When the Hedgehog caught sight of the Hare, he bade him a friendly good morning. But the Hare, who was in his own way a distinguished gentleman, did not return the Hedgehog’s greeting, but said to him, “How do you happen to be running about here in the morning?” - “I am going for a stroll” said the Hedgehog. “A stroll!” said the Hare with a smile. “It seems to me that you might use your legs for a better purpose.” This answer made the Hedgehog furiously angry, “You seem to imagine you can do more with your legs than I with mine.” - “That is just what I do think,” said the Hare. “That can be put to a test,” said the Hedgehog. “I wager that if we run a race, I will outstrip you.” To that the Hare agreed willingly for he was very confident in his quickness.

The next day the Hedgehog reached the field where the Hare already waited for him. “Shall we start?” said the Hare. “Certainly,” said the Hedgehog. The hare counted, “Once, twice, thrice, and away!” and went of in a whirlwind down the field. The Hedgehog, however, only ran about three paces, and then he wrote a unit test, so he would not get lost in the high grass of the field. After another three paces, he stopped again to write another unit test. And so he went forth for quite some time. The Hare, however, must had been running in circles and to his disbelief met with the Hedgehog. “That cannot be done fairly,” he cried “I was ahead of you Hedgehog the whole time, I shall try to run even faster!” The Hare went of like the winds storm, so that he seemed to fly, but the Hedgehog did not change his pace.

The Hare must had been running in circles many times, but never reached the top of the field. When the sun was already setting, the Hedgehog found the Hare lying dead in the middle of the field. The Hedghog, however finished the race quite before afternoon.

The moral of this story, however, is, firstly, that no one, however great he may be, should permit himself to ignore test coverage for the sake of velocity.

The Wolf and the Seven Features

There once was a Wolf who had written a very efficient database layer for his use case. The database layer implemented all features that the Hare had demanded in his user story. Satisfied with himself the Wolf said: “I have implemented all seven features and I can see they are good. But still, I have to test them!” So he went forth and created a test data image for his use case. He added a row for when there are all piglets in wooden homes, and one row for when only one piglet is in a wooden home and the others are in a stone home. He added many more rows like this. And when he was finished he started to write his test. With a single test he covered all features at once! The Wolf said: “This is very elegant. I tested all seven features in one big gulp!”

But then the Hare came and said: “Well done Wolf, you have implemented all seven features. But now I found out that there will never be all piglets in one home. You have to change your implementation!”. “No problem”, the wolf thought and quickly changed the database layer to fit the new needs. But lo! Now the test was broken and it was very hard to fix. The test tested all seven features at one stroke but now it tested features that were not correct anymore! Oh dear, what should the Wolf do now?

When the clever Fox heard of the Hare's misery he came and said: “Wolf, why do you create a big test image? You can create exactly the data you need for each test! And write many small tests. One for each feature!” What a wonderful idea that was. The Wolf added just the case that there is one piglet in the wooden house and tested exactly this in a dedicated test. Now the tests even served as a documentation of the features the Wolf had implemented. When the Wolf showed this to the Hare, he was very satisfied and thanked the Wolf many times. As Customers always do when they see that a software developer did a good job.

The Bear and the Grasshopper

There once was a Bear who had written a very big and very cozy backend for himself. “This backend is a very good one for a big Bear like me. It will suit me very well, and now I shall sleep for the winter!”, the Bear said to himself. He gave a big yawn and fell asleep.

But lo! A little Grasshopper jumped into the backend and hopped onto the Bears nose. This startled the Bear from his sleep and from the corner of his eyes he saw the Grasshopper jump away. “A bug in my backend! How can this be!”, the Bear growled with anger. He slammed his paw into the direction where he last saw the Grasshopper hop. “Let this be a lesson for you, little bug. And do not disturb my slumber again!”, the Bear growled and fell asleep once more.

Some time passed, but then the little Grasshopper came back. He was just hiding behind another feature! He jumped into databases table and onto the belly of the Bear. This woke the Bear from his sleep again and made him more furious than the last time. He hit his paw against all the tables and even added debug logging to the controllers. But he could not catch the little bug.

Then a Hedgehog who was disturbed by the noise came to the backend, “Bear, why do you make such a noise? Shouldn’t you be fast asleep?”. “There is a bug in my backend and I cannot catch it. When I look to the left it jumps to the right. And when I look here it jumps there.”, the Bear answered. The Hedgehog thought a moment for himself and finally said: “Bear you should write tests. Test everywhere you could see the bug and when you have tested all places he will be gone”. The Bear considered this and said, “Bears do not write tests and I do not see why this should help me. But I will do as you ask, for I am weary now and cannot sleep with a bug in my backend.“ And lo! After the Bear has written all his tests the Grasshopper had no place to hide anymore and was never seen again.

I hope you had as much fun reading these stories as me writing them. This post was sponsored by itemis AG. That's the place were I work and drink.

Discussion (0)