-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallel testing recreate-databases and seeding / setUpProcess hook #36068
Comments
The reason why "ParallelTesting::setUpProcess" is not being called is because you are overriding the runner. You should not do that, as Laravel already has his own runner. Are you using any database testing traits? |
Here's my output when I remove the "--runner WrapperRunner" parameter: |
What paratest version are you using? |
"brianium/paratest": "^4.0", |
My bad. I was using an outdated version of paratest. I've upgraded both paratest and phpunit libs to the latest version. Now when I run I've tried to force the tables creation and then re-run using 2 processes: By doing that, I've got my default DB wiped/recreated and it also created two new ones: DB_1 and DB_2. However, only the default one was seeded. DB_1 and DB_2 just got migrated without any data, and due to that, every test failed. |
Thanks for the detailed answer. Before we actually find a solution for this. Could you tell me how would you "seed" your testing database before without parallel testing? |
Before, my setup was basically one separated DB, assigned on a .env.testing file. I have a custom command called "project:setup" that migrates and seeds using a custom seeder class. project:setup command:
the db:seed just calls a DatabaseSeeder class that does the following:
By doing that, we just populate a few tables with essential data needed for testing purposes, and then we were able to test using phpunit/artisan and --env=testing |
In case it helps, I am having similar issues. There is also a separate testing database configured in an In my case I would also need to run a custom Artisan command for each database that is being created: I used to do it with a composer alias: "scripts": {
"test": [
"php artisan migrate --env=testing --no-ansi",
"php artisan bootstrap:application --env=testing --no-ansi",
"./vendor/bin/phpunit --testsuite Unit",
"./vendor/bin/phpunit --testsuite Feature"
],
} So I would assume this would work: "scripts": {
"test": [
"php artisan migrate --env=testing --no-ansi",
"php artisan bootstrap:application --env=testing --no-ansi",
"@php artisan test --parallel"
],
} But it does not add the roles/permissions/default users the bootstrapper creates. Calling the bootstrapper from within |
The I am assuming you all are using the "DatabaseTransactions" trait correct? If yes, would a public function boot()
{
ParallelTesting:: afterTestDatabaseCreated(function () {
Artisan::call('db:seed');
});
} |
@nunomaduro Yes, I am using the DatabaseTransactions trait. An On subsequent runs, would the data still be there that was inserted during the initial run? Or would that data be cleaned up by the If the latter is the case, the "seeded" data would be gone, and the "bootstrapper" would need to run again. But that would not happen because the databases are already created and thus the tests would fail due to missing data. Does that question make sense? If the data would be persisted for future runs, I don't see any issues and that would be a great solution. But if we would have to use the |
The seeded data would be there, as the On the next If you use, |
In that case it really sounds like a great solution! It will only run when needed and ensure the data is present. Great thinking @nunomaduro ! |
@driesvints, correct me if I am wrong, but this is not yet actually implemented in the framework yet, so should the issue not remain open for now? |
@johanvanhelden this issue tracker is mainly for tracking bugs, not new functionality. |
Temporary workaround for everybody running into the same problem. Create a migration with: class SeedTestDatabase extends Migration
{
public function up(): void
{
if (App::environment('testing') && ParallelTesting::token() !== 1) {
Artisan::call('db:seed');
}
}
} |
@nunomaduro any updates on this? |
@introwit not sure what updates you're expecting? |
@driesvints My guess is the suggested fix Nuno mentioned here: #36068 (comment) I was also actually hoping it is something that was being worked on as it is the only thing preventing me from using parallel testing. |
yep what @johanvanhelden said 👆🏽 |
@introwit @johanvanhelden @daniloesser Let me know what do you think: #36301. |
@johanvanhelden, @introwit I've tested and it's working as expected. @nunomaduro , Thank you again for this improvement! |
@nunomaduro one thing I did not understand, on each run of |
Description:
I need to prepare the database to be used by my application's tests (create and seed). So I've added a "db:seed" call using the ParallelTesting facade, to the AppServiceProvider Class - boot method, as a hook, as documentation suggested.
I was expecting that the ParallelTesting::setUpProcess function would have been triggered during each database creation, seeding them and making them ready for tests. But I was not able to. The second hook, ParallelTesting::setUpTestCase works good, but I don't want to seed for every test, I'd like to seed once and only re-seed during the database creation process.
Steps To Reproduce:
1 - Insert some code to seed the database inside the ParallelTesting::setUpProcess hook.
1- run "php artisan test --parallel -p 2 --testsuite=Feature --runner WrapperRunner --recreate-databases tests/Feature" and notice that the code inside the setUpProcess is never called and all my tests are performed without any data from the seeder.
The text was updated successfully, but these errors were encountered: