-
Notifications
You must be signed in to change notification settings - Fork 0
Testing
Laravel has support for automated unit and feature testing through phpunit.
All of the relevant objects should make use of unit testing and the abstract methods should make use of feature testing where ever possible. Please avoid submitting a pull request for a back-end feature without these tests. If you do, your pull request may not be accepted.
Use php artisan make:test
in the CLI to generate a new test file with the required frameworks.
Or use the --unit
flag to create a test file in the unit test suite php artisan make:test --unit
.
Test files must use the following naming cnvention ObjectnameTest.php
or FeaturenameTest.php
. If you do not meet this phpunit will ignore the file.
Methods require the following name TestMethodname()
. Methods that do not use this name will be ignored.
Use the following commands to run the relevant tests:
- Run all tests:
vendor/phpunit/phpunit/phpunit
. - Run feature tests:
vendor/phpunit/phpunit/phpunit --testsuite Feature
. - Run unit tests
vendor/phpunit/phpunit/phpunit --testsuite Unit
.
Normally a test file should represent all the tests for one specific object or set of methods. For example, the users unit test should cover tests for creating and retrieving a user as well as all of the methods within the user object.
For this example, this test will only check that we can send an email via the SMTP driver to mailtrap.
Note For this test to pass it must result in the value being false. You can use similiar asertions to the ones used in Java or C#.
To run this command enter vendor/phpunit/phpunit/phpunit
into the CLI.
<?php
namespace Tests\Feature;
use Mail;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MailTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testSendEmail()
{
try{
Mail::raw('Text', function ($message){
$message->to('[email protected]');
});
$error = false;
} catch (Exception $exception){
$error = true;
}
$this->assertFalse($error);
}
}