The Rule Of Testing 0, 1, 2

We can keep things simple, but effective.

David Rodenas PhD
Dev Genius
Published in
5 min readJun 24, 2023

--

Picture this: you are writing a test, and the behavior of that test deals with multiple elements, how many elements do you use?

It’s a common question, and the answer isn’t always straightforward. Too many elements and your test can become unnecessarily complex. Too few, and you might not fully evaluate the behavior you’re interested in. So, what’s the golden number? Or, more accurately, what’s the golden set of numbers?

Enter the Rule of Testing 0, 1, 2. This rule is a simple yet effective principle that can guide your testing strategy in many scenarios, especially when dealing with elements in a collection, such as a list or an array.

Zero

This is the most obvious number, if you have nothing, you have zero. There is no much to choose.

Testing how your application behaves with zero elements can reveal how it handles empty inputs or lack of data. In many situations, this is a special case that your code needs to handle correctly. Whether it’s a shopping cart without any items, a user without any followers, or a hot air balloon ride booking system with no rides booked, these situations are real-world cases that your software must be prepared to handle gracefully.

 Scenario: No booked rides
Given I have not booked any rides
When I view my booked rides
Then I should see a message saying "You have not booked any rides yet. Check out our upcoming balloon adventures!"

One

One is more tricky than it seems. You may think: it is when you have one element. Well, this is right, and wrong. The one rule applies to two different cases.

Firstly, the obvious case, it can mean exactly one element in the collection — the minimum amount of data your functionality can process. This scenario can reveal whether your application can handle basic inputs. In our examples, this could be a shopping cart with one item, a user with one follower, or a hot air balloon ride booking system with one ride booked.

Scenario: One booked ride
Given I have booked one ride for "Grand Canyon Balloon Ride"
When I view my booked rides
Then I should see a list with 1 ride…

--

--

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by David Rodenas PhD

Passionate software engineer & storyteller. Sharing knowledge to advance our skills. Join me on a journey of discovery in the world of software engineering.

Responses (1)

Write a response