When tests randomly fail in Laravel...


1 min read

One thing I learned today, or actually was reminded of, that when tests begin to fail randomly on reruns, it is often that the factory of the model used may have been configured to use ->randomElement([...]) and therefore on certain combination that wouldn't occur on production flow is causing the tests to fail.

I first encountered this a couple of years ago and was pulling my hair trying to understand why this seemingly abnormal behaviour was happening. From wiping out database, to setting up a dedicated test instance, until I git bisect that it was when I updated my factory class.

Hopefully writing this will help me remember this sooner in the consecutive times which I'm afraid gonna happen.