zhaopinxinle.com

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?

Example of poor test data in a unit test

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

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Exploring Life’s Expectations: A Journey Beyond Boredom

A deep dive into life’s expectations, AI, gratitude, and meaningful pursuits.

Navigating Stigma: A Personal Journey Through Health and Identity

A personal reflection on taking PrEP, confronting stigma, and reclaiming health and empowerment.

Unlocking the Secrets: A Three-Step Strategy for Earning 6 to 7 Figures

Discover the proven three-step strategy top creators use to earn six to seven figures annually, leveraging social media and email marketing.

# Bitcoin's Immediate Future: Analyzing Prospects for the Next 24 Hours

A detailed look at Bitcoin's potential movements in the next 24 hours, considering current market trends and resistance levels.

Understanding iOS Application States and Lifecycle Transitions

Explore the various states of iOS applications and their transitions, including background rules and lifecycle methods.

Invaluable Life Lessons from Richard Feynman: Insights for Life

Discover five essential life lessons from Richard Feynman that emphasize personal growth, skepticism, and the joy of learning.

Mastering Your WiFi Password Retrieval with Netsh Made Easy

Learn how to effortlessly retrieve your WiFi password using the command-line tool Netsh, ensuring you stay connected without hassle.

What Americans Need to Understand About Covid-19 Today

Key insights from epidemiologist Marc Lipsitch on Covid-19, addressing confusion and emphasizing the importance of public health measures.