• Skip to primary navigation
  • Skip to main content

cultivate

  • Home
  • Services
  • Products
  • About us
  • Blog
  • Newsletter
  • Contact

Test Anti-pattern: Copy-pasting real data as test data

Over the years, I’ve observed developers applying dozens of anti-patterns to testing.

The result: An expensive test suite that is not fun to work with.

Here is one of these test anti-patterns:

๐Ÿ›‘ Test Anti-pattern ๐Ÿ›‘

Copy-pasting real data as test data


Tests often contain test data that is unnecessarily complex or elaborate.

โŒ avoid:

it('loads user plan', async () => {
  fetch.mockResolvedValue({
    id: '603064',
    firstname: 'Alysa',
    lastname: 'Patton',
    email: 'apatton0@myspace.com',
    bio: 'I am a software developer with advanced testing skills. I hate writing tests first. But my code coverage is usually around 70%.',
    followers: 20,
    following: 3,
    plan: {
      name: 'Medium',
      quota: 1000,
    },
  })
  expect(await loadPlanForUser('603064')).toEqual('Medium')
})

Most of the above test data is not required to prove that the tested code does what it is supposed to do.

โœ… prefer:

it('loads user plan', async () => {
  fetch.mockResolvedValue({ id: '1', plan: { name: 'Medium' } })
  expect(await loadPlanForUser('1')).toEqual('Medium')
})

Using actual test data in tests creates several problems:

Tests with real data are hard to understand

Tests that use real data will likely take more time to understand. When reading the test, we must assume that the data is essential. But all this unnecessary data distracts from what the test is actually trying to accomplish.

Tests with real data drag development speed down

Using actual data increases the test file size and, thus, the cognitive load required to comprehend the code. Having to weed through test code, as the one in the negative example above, will tire developers faster and drag the overall development speed down.

Tests with real data are more expensive to maintain

The larger the average file size, the more expensive it will be to maintain the project. Everything takes longer: Scrolling, reading, trying to make sense of the test code. It might only be a second here or there, but the lost performance accumulates quickly to several developer weeks per year.

Tests with real data discourage refactoring

A test with real data suggests that the data is important. Other developers will likely not question that. The problem is that the increased complexity discourages refactoring. The result: The code base deteriorates further.

Tests with real data potentially leak sensitive data

And last but not least, copy-pasting actual data into test files might accidentally expose sensitive data such as personally identifiable information (PII) or secrets.

So, don’t copy-paste real data as test data!

See you around,
-David

Test-first

Master the Art of Test-driven Development (TDD) and learn how the Top 1% of Developers create software.

Enroll now

Connect

  • Email
  • LinkedIn
  • Phone
  • YouTube

Legal

Imprint
Privacy Policy

Copyright © 2023 ยท cultivate GmbH

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. ACCEPTCookie settingsRead More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT