Testing Flashcards
By default, your application’s test directory contains two directories, Unit and Feature. what are Unit tests used for?
Unit tests are tests that focus on a very small, isolated portion of your code. In face, most unit tests probably focus on a single method. Tests within your “Unit” test directory do not boot your Laravel application and therefore are unable to access your application’s database or other framework services.
What are Feature tests and what are they used for?
Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint. Generally, most of your tests should be feature tests. These types of tests provide the most confidence that your system as a whole is functioning as intended.
When running tests, Laravel will automatically set the configuration environment to testing because of the environment variables. Laravel also automatically configures the session and cache to the array driver so that no session or cache data will be persisted while testing. Where can you define other testing environment configuration values?
The testing environment variables may be configured in your application’s phpunit.xml file, but make sure to clear your configuration cache using the config:clear Artisan command before running your tests!
How can you create a new feature test?
php artisan make:test UserTest
How can you create a new unit test?
php artisan make:test UserTest –unit
Run a test using Artisan!
php artisan test
What do you need to do to split up a test run across multiple processes simultaneously?
First, you should install the brianium/paratest Composer package as a “dev” dependency. Then, include the –parallel option when executing the test Artisan command:
composer require brianium/paratest --dev php artisan test --parallel
By default, Laravel will create as many processes as there are available CPU cores on your machine. However, you may adjust the number of processes using the –processes option:php artisan test --parallel --processes=4
As long as you have configured a primary database connection, Laravel automatically handles creating and migrating a test database for each parallel process that is running your tests. The test databases will be suffixed with a process token which is unique per process. By default, test databases persists between calls to the test Artisan command so that they can be used again by subsequent test invocations. How can you re-create the databases?
You may re-create them using the –recreate-databases option:php artisan test --parallel --recreate-databases
What is Laravel Dusk?
Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your local computer. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilize any other Selenium compatible driver you wish.
How do you set up Laravel Dusk?
To get started, you should install Google Chrome and add the laravel/dusk Composer dependency to your project:
composer require laravel/dusk –dev
If you are manually registering Dusk’s service provider, you should NEVER register it in your production environment, as doing so could lead to arbitrary users being able to authenticate with your application.
After installing the Dusk package, execute the dusk:install Artisan command. The dusk:install command will create a tests/Browser directory, an example Dusk test and install the Chrome Driver binary for your operating system:
php artisan dusk:install
How can you create a Dusk test?
Use the dusk:make Artisan command. The generated test will be placed in the tests/Browser directory:php artisan dusk:make LoginTest
Most of the tests you write will interact with pages that retrieve data from your application’s database; however, your Dusk tests should never use the RefreshDatabase trait. The RefreshDatabase trait leverages database transactions which will not be applicable or available across HTTP requests. What should you use instead?
Instead, you have two options: the DatabaseMigrations trait and the DatabaseTruncation trait.
What does the DatabaseMigrations trait do?
The DatabaseMigrations trait will run your database migrations before each test. However, dropping and re-creating your database tables for each test is typically slower than truncating the tables.
What does the DatabaseTruncation trait do?
The DatabaseTruncation trait will migrate your database on the first test in order to ensure your database tables have been properly created. However, on subsequent tests, the database’s tables will simply be truncated - providing a speed boost over re-running all of your database migrations. By default, this trait will truncate all tables except the migrations table. If you would like to customize the tables that should be truncated, you may define a $tablesToTruncate property on your test class. Alternatively, you may define an $exceptTables property on your test class to specify which tables should be excluded from truncation.
Run a browser test!
php artisan dusk
If you had test failures the last time you ran the dusk command, how can you save time when wanting to run another test?
You may save time by re-running the failing tests first using the dusk:fails command:
php artisan dusk:fails
How can you create a browser instance for your Dusk tests?
To create a browser instance, you may call the browse method from within your Dusk test:
class ExampleTest extends DuskTestCase { // ... $this->browse(function (Browser $browser) use ($user) { $browser->visit('/login') ->type('email', $user->email) ->type('password', 'password') ->press('Login') ->assertPathIs('/home'); // ... }
How can you create multiple browser instances if needed?
Just pass multiple browsers as arguments and give them whatever attributes you need:
$this->browse(function (Browser $first, Browser $second) { $first->loginAs(User::find(1)) //... $second->loginAs(User::find(2)) // ... $first->waitForText('example') // ... });
For navigation, which method lets you navigate to a given URI within your application?
$browser->visit('/login');
How can you visit a named route?
$browser->visitRoute($routeName, $parameters);
How can you navigate back and forth?
$browser->back(); $browser->forward();
How can you refresh the page?
$browser->refresh();
How can you adjust the size of the browser window?
$browser->resize(1920, 1080);
How can you maximize the window?
$browser->maximize();