subscribe via RSS
Personal Details in Tests Are an Unnecessary Distraction
One of the first things you have to do in a unit test is to decide which values to pass to your system under test. The task seems very straightforward, but it is not that easy after all. Have you ever seen funny data values? Like this one:
val name = "Mickey"
val surname = "Mouse"
sut.register(name, surname) mustBe successfulOr maybe not so funny, but your colleague’s name?
val name = "Vaidas"
val surname = "Pilkauskas"
sut.register(name, surname) mustBe successfulWhile a good sense of humor is a great strength in social life, it easily
distracts us from the problem we are solving. In tests, it is just an additional
load for our brains.
But the second example isn’t related to humor. “What’s wrong with it?” you may ask.
The problem here is the social impact of personalization.
Imagine a situation where this code was written by a senior developer and he has used
his name and surname in the test data.
A person who just joined the company needs to
make changes to this test as part of her new task. If she wants to change it, she
may wonder if she is allowed to change the name, if it has any meaning, or if there is
a practice of naming data after yourself. Would it be offensive if she deletes
unneeded tests with the senior developer’s name and then adds a new one with her name?
This looks like a small issue - it is easy to ask that person, right? But what if he
is on a month-long vacation? Or maybe there’s another reason why it is hard to reach
him. Situations like this add extra load for our brains.
Personal details prevent us from cleaning up after ourselves. And they must be avoided.
In addition, irrelevant details disguise the real meaning of test data and thus fail to
communicate the test’s intent. For example, it is not clear if we are working with
a person that has to be our employee or not.
Test data values should communicate their meaning well. I use boring names like
CustomerName, CustomerSurname. You may develop a different naming convention;
it does not really matter as long as it helps communication. If I need a list of
multiple names, I suffix values with numbers - TestCustomer01. It helps when
tracing failed tests so that you can quickly spot if your naming maps to the correct
order of elements in the resulting list.
Vaidas Pilkauskas