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 = described_class.new(name: Faker::Name.name)
  expected = user.name
    .upcase
    .split(" ")
    .map { |part| part[0] }
    .join("")

  expect(user.initials).to eq(expected)
end
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 = described_class.new(name: "Daniel Craig Jones")

  expect(user.initials).to eq("DCJ")
end
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)

Collapse
 
skynet31 profile image
alexandru

Does this apply to PHP as well?

Collapse
 
hsalem profile image
Hassan

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