Search code examples
testinglaravel-8php-pest

Laravel - Make a check before the command "php artisan test"


I need to do a check before launching the php artisan test command.

I need this because I currently have 2 databases for DEV, one local and one shared with my collaborators. I want to prevent the tests from running on the shared database.

I already saw that I could create a custom command with Laravel but I would like to know if there was a better solution ...

Thanks in advance for your feedback


Solution

  • If you have .env.testing file & define specific database name & credentials there, then test will use this file. For more restriction you can add below code block on your TestCase abstract class & extends that class on all of your Test Case classes.

    abstract class TestCase extends BaseTestCase
    {
        use CreatesApplication;
    
        protected function setUp() :void
        {
            parent::setUp();
            
            if ( !file_exists(__DIR__.'/../.env.testing') ) {
                throw new \Exception('!!! Create .env.testing file !!!');
            }
        }
    }     
    

    And in your test case classes

    class AuthTest extends TestCase
    {
    }