Rambling my thoughts and experience

Unit Testing environment set in laravel 5.1

Posted by Shailesh Davara on January 28, 2016 | 2 Minute Read

Laravel comes with many good features. One of the best feature, I liked a lot is ‘testing’ inside a framework. We don’t need to add any new package. It comes out of the box. There are few points for a good testing likes of having a separate database, run migrations and impersonate development environment etc. Laravel allows you to cover all those points in your testcases.

From laravel 5, it introduces .env file. That is a plain text file with the key value pair. We can have different .env file inside an application for the different environment. When we run testcases, laravel runs it under ‘testing’ environment. To alter testing environment variables, we can edit ‘phpunit.xml’ file in the root folder of our application.

Below is my ‘phpunit.xml’ file.

<phpunit backupGlobals="false"
         stopOnFailure="false" >

    // we can define as many testsuite as we want.
        <testsuite name="Application Test Suite">
        <testsuite name="small">
        <testsuite name="medium">
        <testsuite name="large">
            <directory suffix=".php">app/</directory>

    // I'd like to log my test results to tap format.
      <log type="tap" target="results.tap"/>

    // This is my testing environment variables.
       <env name="APP_ENV" value="testing"/>
       <env name="DB_DATABASE" value=":memory:"/>
       <env name="DB_CONNECTION" value="sqlite"/>
       <env name="DB_DEFAULT_CONNECTION" value="sqlite"/>

You may have noticed that I’ve listed few environment variables in ‘php’ section. The aplication will try to find all environment variables in this file, if they don’t find, it will try to get from .env file of the application. For more configuration options, you can visit unit test documents.