I am trying to run laravel feature tests in parallel mode as it is in document. My phpunit.xml is configed as below :
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
</php>
and I am running the tests with this command :
php artisan test --parallel
The result is all tests fail with this error message :
PDOException: SQLSTATE[HY000] [1049] Unknown database 'mydbname_test_11'
should I create this database before starting test or laravel creates that automatically ?
Yes, the database must exist before you run your test. another thing to note is that parallel unlike PHP artisan test, will not migrate your database for you. So what I found works best is to migrate my database before running the test in parallel.
When running tests in parallel mode with Laravel, it is necessary to create a separate database for each parallel test process. Laravel will not create these databases automatically, so it is up to you to ensure that they exist before running your tests. You can either create the databases manually, or you can use a tool like PHPUnit's #database annotations to automatically create and tear down the necessary databases before and after each test run.
In your phpunit.xml file, you can specify the DB_DATABASE and DB_USERNAME environment variables to control which database Laravel should use for testing. For example:
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="mydbname_test_%s"/>
<env name="DB_USERNAME" value="myusername"/>
</php>
The %s in the DB_DATABASE value will be replaced with the current test process number, so each test process will use a different database. You will need to make sure that a database with the appropriate name exists for each test process before running your tests.
You can then use the #database annotation in your test classes to control the creation and destruction of these databases.
Related
I have app which uses laravel 5.0 and phpunit 4.8.
I have test, which looks like this:
$this->call('POST', 'api/get_user_by_id', [
'token' => 'token',
'user_id' => 3,
]);
It connects to may main database and get data from it. But I want test connect to other, test database and work with it. How can I do it?
You can configure your test environment in your phpunit.xml file:
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_HOST" value="localhost"/>
<env name="DB_PORT" value="3306"/>
<env name="DB_USERNAME" value="username"/>
<env name="DB_PASSWORD" value="password"/>
</php>
I'm having this weird problem that doesn't always happen and sometimes php artisan config:cache fixes it.
Sometimes when I run phpunit test, it loads the local environment configuration and messes up the database. Changing values in phpunit.xml doesn't seem to have any effect at all.
Any idea why this happening and how to fix this?
I've tried overriding the createsApplication method and manually set the environment variables, clearing cache, caching config, PHPStorm test suit with custom configuration.
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
Laravel v5.5.14
PHPUnit v6.4.1
I have a Laravel application with a number of feature tests. My base TestCase uses the RefreshDatabase trait for triggering a DB refresh before each test, and the env variables in my phpunit.xml looks like so;
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
The intention being to leave the development mysql DB connection alone and run the tests using sqlite in memory. Despite these settings (and clearing cached config etc) the DB refresh always refreshes the mysql connection DB.
I've looked into the functionality within RefreshDatabase and debugged the refreshDatabase method, which contians;
$this->usingInMemoryDatabase()
? $this->refreshInMemoryDatabase()
: $this->refreshTestDatabase();
And usingInMemoryDatabase always returns false. Looking at the content of that method, when retrieving the DB_CONNECTION from config it always returns mysql, even though it should be being overridden by the env variable above.
What is going on here?
Run php artisan config:clear before running the tests to clear the cache to make sure the config variables are not cached from a wrong environment. If this doesn't help run PHPUnit using your IDE (e.g PHPStorm) and specifically select your project phpunit.xml
Im trying to test mail sending in laravel 5.1 application.
I need run tests on prod server.
I need MAIL_DRIVER=log at app environment for running test.
And MAIL_DRIVER=smtp for production in my .env file.
I edited phpunit.xml at application root folder and added line:
<env name="MAIL_DRIVER" value="log"/>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="log"/>
</php>
The problem is when I running test it still use MAIL_DRIVER=smtp
Any idea what is wrong?
Try to redefine mail transport type directly. Changing environment constants will do nothing (or share the code where MAIL_DRIVER is used).
Most of my classes include loading env variables for configuration. When I was coding I thought vlucas/phpdotenv ignored the .env file on testing environments since there is already a conf file like phpunit.xml where you define them.
Is it possible to make dotenv ignore .env when on testing environment?
Testing will use your .env file, but you can override any of its settings in phpunit.xml.
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_DRIVER" value="sqlite"/>
<env name="CACHE_HOST" value="localhost"/>
<env name="SESSION_HOST" value="localhost"/>
</php>