I am running unit tests on Laravel 5
and as the questions explains itself, App::runningUnitTests()
is returning false during unit tests run. Why is that happening?
My phpunit.xml
has this:
<env name="APP_ENV" value="testing"/>
My PHP
version is 7.1
.
3
Answers
As it turns out config cache was the issue.
php artisan config:clear
did the trick. Earlier, I was tryingphp artisan config:cache
.add the following lines to phpunit.xml file:
The App::runningUnitTests() method in Laravel 5 returns true if the current environment is set to testing. This is usually set in the phpunit.xml file using the APP_ENV environment variable. In your case, you have set this variable to testing, so it should be returning true during unit tests run.
If you’re still getting false when running unit tests, there could be a few reasons why this is happening:
Your test is not actually running in the testing environment. Double-check that your test class extends TestCase and that you’re running your tests with the –env=testing option.
You’re using a cached configuration file. Laravel caches the configuration files for performance, and this cache is only cleared when you run php artisan config:clear. If you’ve made changes to your configuration files since the last time you cleared the cache, you may be running your tests with outdated configuration values.
You have a custom App::runningUnitTests() implementation. If you’ve overwritten the runningUnitTests method in the App class, your custom implementation may be causing the unexpected behavior.
There is a bug or misconfiguration in your Laravel installation. This is unlikely, but it’s possible that something is not set up correctly in your Laravel installation. Try creating a new Laravel project and see if you get the same behavior.
In any case, I recommend checking the environment variables during the test run to make sure that APP_ENV is set to testing. You can do this by using getenv(‘APP_ENV’) or $_ENV[‘APP_ENV’]. If you’re still having trouble, try running your tests with the –debug option to see if you can identify any issues.