Yes, the usual approach is to set an environment variable, a global variable, or a constant in phpunit.xml configuration file (doc at https://phpunit.de/manual/6.5/en/appendixes.configuration.html)
Add the following section into phpunit.xml' root <phpunit> element to define constant IS_TESTING:
<php>
<const name="IS_TESTING" value="1"/>
</php>
then in your PHP code, you can do the following:
if (defined('IS_TESTING')) { ... }