Test factories should not have randomness, but... Why?
Table of contents
I had a discussion with a colleague the other day about randommess in FactoryBot objects.
They were in favor of having randomness in their objects, their main argument being:
If we have random values, eventually we are bound to find the edge cases.
I was against, my main argument being a flavor of:
Tests should be deterministic
My inability to better explain why has prompted this post to produce a though out explanation as to why.
Trust
Randomness in setting the context of tests makes our tests untrustworthy, and:
Tests we cannot trust are a waste of time, brainpower and computation (in order of decreasing importance).
We waste time having to run them multiple times.
We waste time having to manually verify facts the tests should tell us.
We waste brainpower trying to figure out if a failing test is a genuine regression or if it is just flakyness introduced by the randomness.
We waste computation, although with a lesser importance in my eyes, we do waste computation if we introduce flakyness.
Hidden information
If we rely on randomness to catch our edge cases, we rob them of the visibility and place they deserve. The next person to come through our tests won't know what edge cases we have faced, and won't know if they are tested.
Unless we log the objects in our tests, chances are the failure message will be really critic
If we are reaching for an object from a factory, we now have to know or lookup what values might vary.
Tests should be deterministic
In closing, I guess my 'one Line argument' remains the same:
Tests should be deterministic
But I sincerely hope someone will be convinced by the expanded arguments above.
Subscribe to my newsletter
Read articles from Pablo Curell Mompo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by