DEV Community

Vasily Polovnyov
Vasily Polovnyov

Posted on

Using Faker is anti-pattern

In my opinion, Faker and randomly generated data is usually an anti-pattern in tests.

First of all, with Faker tests become nondeterministic: run - crashed, run again - passed. Such flaky tests are extremely difficult to debug and annoying on CI.

Secondly, random data slows down tests. Especially in factories: I worked on a project where switching from Faker to sequence { ... } accelerated the test suite by 12%.

Thirdly, Faker overuse leads to code duplication in the tests. For example, take a look at the test method that returns user's initials:

it "returns initials"
  user =
  expected =
    .split(" ")
    .map { |part| part[0] }

  expect(user.initials).to eq(expected)
Enter fullscreen mode Exit fullscreen mode

This is an incomprehensible and confusing test: if the #initials method changes, the changes will have to be duplicated in the test as well. A better way would be to write it like this:

it "returns initials"
  user = "Daniel Craig Jones")

  expect(user.initials).to eq("DCJ")
Enter fullscreen mode Exit fullscreen mode

I suggest you to take a closer look at Faker in your test suite: are you sure you need it?

More on this topic:

Top comments (2)

skynet31 profile image

Does this apply to PHP as well?

hsalem profile image

Yes, random data in the tests make them unrepeatable and flaky.