Software testing is done to provide stakeholders a quality rich product and service. It also helps us to understand the risks of software implementation. In this blog we will see the importance of testing and the ways of implementing in Laravel. We have also included our open source support for testing.
Importance of testing
- Testing is essential to know how your software product respond to different kinds of scenarios.
- Testing the software or application helps us in finding the bugs.
- It saves us lot of time where we will have to test repeatedly for checking the same result. That is one of the main reasons to automate Testing and considering testing as a “Process”.
- Even in “Software Development Life Cycle” testing is a process that is given a great importance and it is defined as a process of “Validating and Verifying” the Software products.
Types of Testing
There are different kinds of Testing that are currently in use today. Among these techniques the widely used Industry Testing Techniques are
- Unit Testing – Testing a single isolatable part or unit of an application. This testing may involve testing a method, class etc..
- Integration Testing – This can be summarised as “Testing the functionalities of components when they are combined”.
Testing implementation using Laravel
Laravel supports unit testing. To run Laravel Tests you need to end the class name with Test. For example, if you are testing PostController ,name your Test case as PostTest.
To do this just run the following command,
php artisan make:test PostTest
Let’s check whether string is present in the webpage or not with Laravel Testing
php artisan make:test ExampleTest
Checking whether a string is present in the webpage:
The most common things done in testing are:
- The basic CRUD operations are successful.
- A default layout is written for testing and each time they copy and paste the same layout class for their future tests.
- REST API Testing with JSON requests and responses.
We have developed a simple package for testing considering the above common things which you can make use of it and build your additional testing on top of it.
Package link : https://github.com/MallowPhp/test
Download and install the package by following the instructions in the URL. After following all the instructions run the following command in your Terminal php artisan You will find a command mallow:test.
Now when you look at the source code of LayoutTest.php in the package(look in github or look in /vendor/mallow/testing/BaseTestcase)
You have eight attributes:
- $token – To save the token. Leave this null as this is used by getToken() method.
- $link – Specify the default path of your resource you want to check. $link = “localhost:8000/topics” If there is “posts” under route prefix group topic. Define as $link = “localhost:8000/topic/posts”
- $table – It indicates the table name. This is for checking whether the data is stored after testing in the table.
- $credentials – Data in JSON to validate user credentials and generate token.
- $data1 and $data_update are JSON objects with the data you give.
- $checkDataExistsInTable and $checkDataAfterUpdate are for checking the data you send is stored in the table you mentioned. Don’t to give this in an array.
You will find four methods:
- First Method getToken() is used for providing authentication token to other methods.
- testStringAndResponseThatExists() is the method that checks the response in database with the string you specify after you save new resource. It returns “OK” if this is successful when you run phpunit.
- testStringAndModifiedResponse() is the method that checks the response in database with the string you specify after you update the existing resource. It returns “OK” if this is successful when you run phpunit.
- testIndex() is the method that checks whether it returns the data with 200 response.
php artisan mallow:test MallowTest
After execution check tests folder in your project root directory. You will find “MallowTest.php” with the file contents defined in “LayoutTest.php” inside BaseTestcase folder(comes with package installation) except the class name here would be replaced with “MallowTest” from “LayoutTest”.
These methods are few samples for testing your “REST API”. You can always feel free to define your LayoutTest.php by replacing the file contents with your contents.
To do this first go to the following path,
Copy and paste your Testcase Layout contents in the file above mentioned. And Don’t forget to define the classname as “LayoutTest”. This LayoutTest will be automatically replaced with the class-name you specify in the command,
php artisan mallow:test NewLayoutTest
Now, ”NewLayoutTest.php” in tests will have the contents you define and with the class name “NewMallowTest”.
There are many assertions available apart from what we have seen. Check Laravel Testing Documentation for more details. Hope this will also help you to get ideas on how we can also create a package and contribute to the open source.