Stop Using Meaningless Test Values!

simeg

Simon Egersand 🎈

Posted on April 29, 2022

Stop Using Meaningless Test Values!

Did you ever find a test where the mock data was a bunch of meaningless "test" strings and 123 integer values? Yeah, me too β€” and it sucks.

Meaningless Test Values 😿

Consider this JavaScript 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);
});
Enter fullscreen mode Exit fullscreen mode

This is a a standard unit test that consists of 1) setup 2) execution and 3) assertion. It looks fine, why are you saying it sucks?

If I found this test in a codebase I would not understand what data is relevant for the test to pass. If I change name of the parent from "test" to "some-name" , would the test still pass? I don’t know. The name of the test might help, but as with other documentation, it will eventually change and be out-of-sync with the code.

Meaningful Test Values 😻

What if we use meaningful test data, how would our test look then?

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);
});
Enter fullscreen mode Exit fullscreen mode

By reading this test I can see that "specific-parent-name" is found both in the parent and in the children and has the prefix specific, which tells me that this value is important for the test to pass. Irrelevant values on the other hand has the value "irrelevant", which tells me I can safely ignore these in this test.

For numbers and booleans you can extract the value to a constant and give the constant a meaningful name.

⚠️ Disclaimer ️

Using strings as IDs is bad practice β€” use integers instead! I used strings to have a clear example.

Conclusion

  • Using "test" and other meaningless values in your tests is bad practice because it does not convey any meaning. This makes the test take a longer time to understand and will slow down your development time, e.g. when you need to refactor or extend your code
  • Instead, use meaningful values. E.g. "specific-something" and "irrelevant"
  • Extract number and boolean values to constants with a meaningful name

Do you agree or disagree? Let’s discuss in the comments 😊


Follow me on Twitter @prplcode

Originally published at https://prplcode.dev

πŸ’– πŸ’ͺ πŸ™… 🚩
simeg
Simon Egersand 🎈

Posted on April 29, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related