Stop Using Insignificant Test Values in Your Code!
Written on
Chapter 1: The Problem with Insignificant Test Values
Have you ever encountered a test where the sample data consisted of meaningless "test" strings and arbitrary integer values? It’s frustrating, isn't it?
Section 1.1: What Are Insignificant Test Values?
Take a look at this JavaScript unit test:
test('should return children of parent', () => {
// mock some data
const mockedParent = {
name: "test",
age: 123,
interests: [
"test",
"test",
],
};
const mockedChildren = [
{
name: "test",
parentName: "test",
},
{
name: "test",
parentName: "test",
},
];
// run the code
const actual = getChildrenForParent(mockedParent, mockedChildren);
const expected = [
{
name: "test",},
{
name: "test",},
];
// assert the results
expect(actual).toBe(expected);
});
While this unit test appears structurally sound, it raises concerns. If I were to find this test in a codebase, the relevance of the data used would be unclear. For instance, if I altered the parent's name from "test" to "some-name," would the test still pass? It's uncertain. Although the name of the test could provide some insight, like other documentation, it risks becoming outdated and inconsistent with the code.
Section 1.2: The Importance of Meaningful Test Values
Now, let’s consider how the test would look if we employed meaningful data:
test('should return children of parent', () => {
// mock some data
const irrelevantAge = 123;
const mockedParent = {
name: "specific-parent-name",
age: irrelevantAge,
interests: [
"irrelevant",
"irrelevant",
],
};
const mockedChildren = [
{
name: "child-name-1",
parentName: "specific-parent-name",
},
{
name: "child-name-2",
parentName: "specific-parent-name",
},
];
// run the code
const actual = getChildrenForParent(mockedParent, mockedChildren);
const expected = [
{
name: "child-name-1",},
{
name: "child-name-2",},
];
// assert the results
expect(actual).toBe(expected);
});
This revised test clearly shows that "specific-parent-name" is present in both the parent and children, with the prefix "specific" indicating its significance for the test's success. Conversely, the "irrelevant" values signal that these can be disregarded.
Chapter 2: Best Practices for Test Values
When dealing with numbers and booleans, it's advisable to define constants with descriptive names.
The first video, "Stop Wasting Money On Pointless Creative Tests - Facebook Ads Tutorial," addresses the importance of meaningful testing in digital advertising.
The second video, "If You Feel Sick DO NOT Test for COVID19 | The Case Against Using Rapid Antigen Tests," discusses the implications of using rapid tests inaccurately.
Conclusion: Moving Forward with Meaningful Values
Utilizing placeholders like "test" and other insignificant values in your tests is detrimental, as they lack clarity. This ambiguity can slow your development process, especially during code refactoring or expansion. Instead, strive to implement meaningful values, such as "specific-something" and "irrelevant," and extract numerical and boolean values into well-named constants.
What are your thoughts? Share your opinions in the comments!
Connect with me on Twitter, LinkedIn, or GitHub.
Originally published at prplcode.dev