No votes yet.
Please wait...

Not taking technological peculiarities of a tested application into account, it must be thoroughly tested for uncaught malfunctions and bugs which can hugely influence the technological process of its functioning. A modern Laravel framework is used exactly for this purpose.

We can surely say that Laravel completely supports PHPUnit test environment in its core. As we know, PHPUnit is the most popular framework for testing the functionality of PHP code. It helps us to easily create 2 types of tests – unit and functional tests.

We will start analyses of the capabilities of Laravel framework from its grounds, namely from the definition of unit and functional testing. As we move forward, we will see a complete picture of the development of unit and functional tests in the application.

Functional and Unit Tests

If you are familiar with interaction with PHPUnit, you should understand that any testing in this environment can be easily divided into 2 categories: unit testing and functional testing.

In the process of unit testing, you have a task to assess a previously established function or a special method. Just as important, you test a part of the logic of a written code at present time.

In the product developed by your quality control companies, you can find the rule that your announced method consists of more than one logic block, so you should think about dividing it into certain methods to make each one include logically correctly built fragment of a program code.

To completely understand the logic of the process of unit testing, you can look at this example of a code:

Example of a Code

Example of a Code

According to written functions, the method performs only one programmed action. It uses ucfirst function to quickly transform one title into another one with the uppercase.

Whereas we use unit test only for verification of correct building of a logical value of a program code, the functional test gives us a possibility to test the correctness of establishing the correct variant of actions. Analyzing this definition more thoroughly, functional testing allows a tester to simulate a set of actions which may be performed by a client inside the utility in order to run his necessary variant.

A good example is that you as QA can perform functional testing for a certain group of input parameters which usually consists of the following steps:

  • Developing the GET query to access the login page;
  • Checking whether a correct page has been displayed;
  • Creating the POST query to immediately send the data to the web page with your login;
  • Checking whether your created session has been successful.

Only in this way you can quickly and efficiently create a functional test case. In the next chapters, we will analyze the simple examples how to efficiently and quickly create simple functional and unit tests in Laravel application.

Delivering the Advance Conditions

Before we start to create the group of actual tests, we should do some things which will interact with our created tests.

First of all, we should create the Post working model and also the migration connected with it. For this you need to write a special artisan command which is exactly responsible for the development of the post model:

A Special Artisan Command

A Special Artisan Command

Right this command allows us to generate the class of the post model and also the database migration connected with them.

A developed class of the post model must look like that:

A Developed Class of the Post Model

A Developed Class of the Post Model

And to make the file of performed migration be in working condition, you should mention a correct way of its location:

Location of the Migration File

Location of the Migration File

Of course, we must save the post title. For this you need to qualitatively process the program code of the file of your post migration to make it have such a structure:

The Structure of a Migration File

The Structure of a Migration File

According to the created scheme, we can see that a special column which is responsible for saving our title with messages has been added.

A Special Column

A Special Column

Then you just need to run the special migrate command in order to create a necessary table with the common database.

Migrate Command

Migrate Command

Then you should completely change the used post model to the following:

A Post Model

A Post Model

 

We have just added a special accessor method which helps us to edit titles of the messages, namely the manipulations which are necessary for your example of the test. All this is also completely true for the post model we analyzed before.

Now our task is to create a file container which will help us to design a necessary functional test script at the final stage of testing.

A File Container

A File Container

A Functional Test Script

A Functional Test Script

Inside the index method, we should try to extract the message identifier from the set of parameters of our query with a future downloading of post model.

Specify the path to the necessary file:

The Path to the Necessary File

The Path to the Necessary File

Unit Testing of the Application in Laravel

Previously, we analyzed step-by-step instruction to configure our primary test. Now we will try to re-create the example which helps to understand how unit testing concept interacts inside Laravel.

Usually, Laravel utility is the artisan command which helps the testers to play with a classical typical class of its unit test.

Let’s run this command in order to create a special AccessorTest class. In this case, it’s very important to have a word “unit” which helps to create a unit test case located in the specified directory of tests/unit.

A Unit Test Case

A Unit Test Case

This manipulation will help us to create the following important class inside it:

A Class

A Class

 

A Class

A Class

Add a “useful” program code to it:

A Program Code

A Program Code

As we can see, a program code completely overlaps with the things we can see in any core of PHP. We just needed to import all dependencies of code from Laravel utility which can help us to connect all API, necessary for the test.

So, we’ve done with unit testing in Laravel!

Functional Testing

Firstly, we need to create preconditions in order to work in the functional test environment (in our case, AccessorTest). As we don’t use a functional expression – unit, all our work will be regarded as a multi-functional test case with shifting to a special catalog – tests/feature.

It will help to create the necessary class:

The Necessary Class

The Necessary Class

Then change everything to the necessary program code:

The Necessary Program Code

The Necessary Program Code

We must repeat once more: this code must look familiar for us, especially if you are familiar with functional testing.

So what we got: first of all, we managed to extract the post from the mentioned database and know that a value inside $db_post_title will change. Then we will have a possibility to simulate the “GET” query and to capture the obtained response into the $response variable.

Then we analyzed the response code inside $response variable with the expected response. According to our basic data, it must be 200 since we must get a real report on GET query. A special title must be also mentioned in the response which starts with uppercase and it’s exactly what we need in the process of interaction with the assertSeeText method.

Some Words about Integration Testing in Laravel

Interaction with integration tests – it’s a combination of performing the established metrics in the process of interaction with a particular visual product. Such test cases are completely different from typical unit tests which test the parts of utility which are independent of each other.

Usually, integration tests allow your application to process particular information blocks (for example, the logic of visiting a particular web page after certain manipulations) and check the functionality of the utility to be responsible for established commands.

In its nature, every integration test interacts with Laravel application in the same way as with a simple black box.

They don’t “care” what is inside the utility, the obtained result is above all.

To conclude, we can say that we have thoroughly analyzed the process of testing in Laravel environment which supports a program structure of PHPUnit in its environment. We studied the bases, analyzed the principles of building unit and functional tests and also understood some peculiarities of testing in the particular functional context of Laravel.

In the process of studying of capabilities of testing in Laravel, we created some good examples which help us to understand how actually to quickly create unit and functional test cases on the basis of established artisan command.

Leave A Comment