Before starting with testing in laravel please check Introduction to testing methods and tools.
Basic Form testing in laravel using PhpUnit
Laravel is built with testing in mind and it has included PhpUnit to support testing of laravel applications. You can check your “composer.json” where you can find “Phpunit” package pre-installed.
When you install the larval new project, it will install PHPUnit package and generate phpunit.xml file which is default testing environment configuration file. You can also define your custom environment configuration variable in this file.
As you can see in above figure there is a directory called tests, which stores all of your test class files.
An ExampleTest.php file is provided in the tests directory. You can run your test by simply typing phpunit command in your terminal.
If you get some error while running phpunit you can install phpunit package globally for all your laravel projects using following command-
composer global require “phpunit/phpunit=5.1.*”
(Make sure you have ~/.composer/vendor/bin/ in your path.)
Example form test in Laravel 5.1
Laravel 5.1 has various methods which make it easy to test your application. In this example, we’ll see how to test simple authentication/login form using phpunit.
Step 1. Create a test class.
You can create a test class using make:test command of the artisan.
‘php artisan make:test TestClassName’
This command will create a LoginTest.php in your tests directory.
Step 2: Define test cases
In simple login form there are 5 basic test cases, which should be tested (you can think of more test cases).
- Login page url exists and working
- If a user submits the empty form he should not be able to login.
- If a user submits wrong values like “abc” in email field, then he should not be able to login.
- If the user enters mismatched username and password, then he should not be able to login.
- If the user enters correct values of username and password, then he should be able to access his account.
Step 3: Write test class and functions. Laravel has various assert methods to check various elements of web pages so I’d recommend you to read Laravel testing documentation. Let’s suppose my Login route will be “auth/login” and dashboard route will be “/”. So in case 2,3 and 4 after submitting login form user should redirect to login route itself. But in case 5, the user is entering the correct value so he should be able to access his account. Test class for this will look like
Step 4: Run test case.
Now, after writing test we will run the test. For now of course we’ll get errors or test will fail because we have not written the class to authenticate user login. Run the test by using phpunit command.
Step 5: This step consists of two recursive processes.
A: Write code to satisfy the test cases
B: Test code against these test cases and modify your code.
After your code passes each test successfully, you can remove all unnecessary blocks of codes and comments from your codebase.
My code for above the test cases is as below.
Lets’s run our test class again and see if our code passes all the tests or not.
6 “.” shows that all 6 test cases are passed successfully.
So here we have tested a basic login form for user authentication using laravel phpunit package. In the next article we’ll see how to perform acceptance and functional testing using codeception.