Constants in unit tests

Igor Budasov
WebDevOps
Published in
1 min readJun 13, 2018

I like to use constant literals in tests instead of constants itself.

Sure, I could use \Products::SHOPIFY_DIRECT instead of shopify_direct string, and on a first glance nothing is wrong with it. Actually it’s fine.

But what if value of constant accidentally changes?

Tests will be fine, because we use the reference to this value (I consider the constant as a reference), but actual value changes, which can lead to unpredictable behaviour.

In case of using constant literal — test will break, and developer at least will know that something goes wrong.

--

--

Igor Budasov
WebDevOps

A traveller. A drinker. A photographer. A blogger. An engineer.