Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

euphogeeza's avatar

How to "php artisan db:wipe" the TESTING database?

Hi, First time posting :-) I am using a mysql test database (as I have views and procedures in the database). When running "php artisan test" the first time my tests pass. If I re-run tests, almost all fail. https://laracasts.com/discuss/channels/laravel/how-to-php-artisan-dbwipe-the-testing-database#

I have to drop all tables and views in the mysql testing database before the tests pass again!

I have found php artisan db:wipe --database=mydb_testing --drop-views command but get an "InvalidArgumentException - Database connection [mydb_testing] not configured." error.

The testing database name is configured in the .env.testing config file. I though about trying php artisan db:wipe --database=mysql... but that might drop the production database as the connection type/name is "mysql". for both!

Content of .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=xxxxxx
DB_PASSWORD=xxxxxxxxxxxxxxxxxxxx

Content of .env.testing

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb_testing
DB_USERNAME=xxxxxx
DB_PASSWORD=xxxxxxxxxxxxxxxxxxxx

How do I run the artisan db:wipe command to drop tables and views from the testing database?

Kind regards Ritchie

0 likes
9 replies
euphogeeza's avatar

Hi @OussamaMater, Wow, that was a quick response. Thanks :-) I've just checked and it looks like all my tests are already using the trait, including the tests that come pre-built in a new project e.g. AuthTest.php

namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class AuthTest extends TestCase
{
    use RefreshDatabase;
    public function test_login_redirects_to_dashboard()
    {
        ...
    }
}
OussamaMater's avatar

@euphogeeza

If you would like to totally reset the database using migrations, you may use the Illuminate\Foundation\Testing\DatabaseMigrations trait instead.

Isn't that what you are looking for? you can use DatabaseMigrations trait instead, I updated my answer above.

OussamaMater's avatar

@euphogeeza yes and keep me updated :) if it resolves the issue please close the thread by setting a "best answer" for future comers :)

euphogeeza's avatar

@OussamaMater Thanks again. I will try tomorrow now as it's nearly midnight here and my bed is calling me :) I will keep you updated and mark the thread appropriately. KR Ritchie

euphogeeza's avatar

Hello again @OussamaMater. I have looked at my problem with a fresh mind today and by limiting the number of tests being run (and therefore the number of error message(s))... I can see that the RefreshDatabase trait is doing the job, but, the tests are failing during the migration stage. I have a view that is not being destroyed after the test is run causing the following error.

   • Tests\Feature\Auth\AuthenticationTest > users can authenticate using the login screen
   PHPUnit\Framework\ExceptionWrapper 

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'musicfullviews' already exists (SQL: CREATE VIEW  ...

It seems I have a different problem to solve. I shall mark your answer as "Best Answer" to close this thread. Thanks for your help Ritchie

Please or to participate in this conversation.