top of page

Test Data in Cypress with Faker


Hard-coded test data works for small demos, but it quickly becomes a problem in real test suites.


Using the same values repeatedly creates unrealistic scenarios and can cause conflicts when tests create or manipulate data.


A better approach is to generate dynamic test data using a library like Faker.


Why Hard-Coded Test Data Is a Problem


Example of common hardcoded test data:

  • "Test article"

  • "John Doe"

  • "Lorem ipsum"


Problems with this approach:

  • Tests always use the same values

  • Real applications contain diverse data

  • Duplicate records can cause conflicts

  • Maintaining test data becomes tedious


Dynamic data helps simulate more realistic scenarios.


Introducing Faker


The Faker library generates realistic random data for testing.


It is widely used in test automation and downloaded millions of times per week.


It can generate:

  • names and personal data

  • addresses and phone numbers

  • company and finance data

  • dates and locations

  • lorem ipsum content

  • job titles and descriptions


Official package:

@faker-js/faker


Installing Faker


Install the package as a dev dependency:

Then import it in the test file:

Now faker functions are available through dot notation.

Example:

Example Without Faker


Before using Faker, test data is hardcoded.

Issues:

  • Every test creates the same article

  • Data lacks variety

  • Duplicate titles can cause problems


Using Faker to Generate Dynamic Data


Now we replace static data with randomly generated values.


Important best practice:

Create constants for faker values so the test uses the same value across the entire flow.

If Faker is called multiple times without a constant, each call generates different data.


Example With Faker

Example generated output might look like:


Best Practices When Using Faker


  • Generate values once and store them in constants

  • Reuse the same value across test steps

  • Avoid calling faker multiple times in assertions

  • Combine faker with API-based test data setup


This ensures stable tests while still generating dynamic data.


Key Takeaway


Dynamic test data improves test realism and prevents conflicts caused by static values.


Libraries like Faker allow Cypress tests to simulate production-like data while keeping the test suite flexible and maintainable.


For larger automation frameworks, this approach is commonly used together with API test data creation and cleanup strategies.

 
 
bottom of page