skip to Main Content

I would like to know if it is possible to set up the testing environment in laravel with a database that is not existing in your pc, for example i have mysql datbase, and i want to set up the test to be in mysqllite is that possible if not, how can i set up the testing environment in laravel with mysql database ?

when i try this :

'sqlite_testing' => [
     'driver' => 'sqlite',
     'database' => ":memory:",
],

i get this error :

IlluminateDatabaseQueryException: could not find driver (Connection: sqlite_testing, SQL: select * from sqlite_master where 
type = 'table' and name = migrations)

2

Answers


  1. Chosen as BEST ANSWER
    'mysql_testing' => [
                'driver' => 'mysql',
                'database' => "testing_db",
                'host' => env('DB_HOST', '127.0.0.1'),
                'username' => env('DB_USERNAME', 'root'),
                'password' => env('DB_PASSWORD', ''),
            ],
    

    i want to the database.php file and set up the mysql environment like this and created a database called testing_db in phpmyadmin and it worked, turns out you can't run a test on databse that is not in your local environment


  2. I don’t know how you came to this conclusion, but there does have ways to use another testing only database when executing unit tests, and you don’t have to create a new driver config for testing.

    Creating a .env.testing in your project root, it also allow you using different host, username or database for testing, it’s simple just as you set a normal .env.

    # In .env.testing file
    ... 
    DB_CONNECTION=mysql  # Also allow you to using different driver for testing.
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=testdb   # You can set testing database here.
    DB_USERNAME=root
    DB_PASSWORD=
    ...
    

    Laravel already set done PHPUnit xml config, it will trying to load .env.testing when running test, if there is no .env.tesing, it will load the .env, you can find more details from hereļ¼šhttps://laravel.com/docs/10.x/testing#the-env-testing-environment-file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search