Introduction to Unit Testing in Laravel

Tram Ho

Foreword

Hello everyone, I am back here. I have joined many projects of the company so far, from projects without installing CI / CD to projects integrating CI / CD into projects. A nice day join a project that is starting phase 2 already, suddenly I have just edited a little api but when pushing the code to the CI reported an error, opening the CI to check it saw no convention error, but again Fail test error message. At that time, it was so confusing, writing tests in Laravel was something, because up to now, my projects do not usually write tests, so I do not care about writing tests. And the writing test started. But then I realized that writing tests is quite important, I have considered writing this test as unnecessary, functional code so that it can run.

What is PHPUnit?

PHPUnit is one of the most well known and highly optimized unit testing packages, which is considered as the first choice of many programmers for fixing various application security vulnerabilities. Its main function is to perform all unit testing in the application. It supports most PHP frameworks including Laravel.

PHPUnit was developed with many simple assertion , plus it gives optimal results when you test each function in your code. But the process of testing the controller, model and form validation can be more complicated than you might imagine.

Unit Test in Laravel

Laravel is a popular framework currently used by many programmers. From a testing perspective, it is also known to include testing when installing frameworks in use. In Laravel there are 2 ways to test, first with Unit testing , second is Feature testing . Unit testing allows us to test model classes, controllers, etc. The goal of Unit test is to check the integrity of the code unit’s processing. Feature Testing allows you to test the API, test the results. returns, tests functionality, performance and load capacity of the application.

If you have already installed the Laravel init project, you can see two available subfolders. That is the tests folder with 2 sub-folders that are the Unit and Feature used for writing tests here.

To clarify, I will give an example of writing unit tests in the next section.

For example unit tests in Laravel

Okay ok we will come up with an example for writing unit tests in a project using Laravel. In every Laravel project, there is already phpunit/phpunit available so you just need to use it. And the first thing in writing tests is to create test classes. These classes are stored in the tests directory of the Laravel project. And they have certain naming rules so that PHPUnit can find each of these test files and run them. For example, you can test the model you can set to UserTest.php . Next you will see the TestCase.php file which is located at the same folder as Unit and Feature . It is basically a file to set up the Laravel environment and the features inside our test. You mean that we will be able to use the functions that support us to write more tests, and those functions are what I will introduce to you in the process of making an example.

To create a test file you will use the command

Let’s try to create a test file to see what it looks like

And Laravel will generate for us a file with the following content:

The name of the test method must be meaningful for what you want to test, there are 2 ways to name the test function

  • test_store_survey ()
  • testStoreSurvey ()

And PHPUnit cannot run tests with protected and private methods, so you have to modify the test function to be public. We can also easily find the phpunit.xml file, PHPUnit will automatically locate the file name named in phpunit.xml or phpunit.xml.dist in the current executable directory. Within this file, we can specifically configure the execution of our test.

First of all, we will look through a bit of the functions mong đợi our results to see if after running the test, it looks like that. You can use 7 PHPUnit assertion functions to look forward to your output

  1. assertTrue ()
  2. assertFalse ()
  3. assertEquals ()
  4. assertNull ()
  5. assertContains ()
  6. assertCount ()
  7. assertEmpty ()

assertTrue () and assertFalse ()

These two functions are intended to allow us to expect after a process of running the test is completed true or false

To run phpunit you run the following command

then you will see the results returned as this has successfully run the test

assertEquals () and assertNull ()

assertEquals() helps us compare the true value after a processing sequence with the value we want.

assertEmpty() helps us check if the desired value is null

And results

You try to change $expect = 'Hoang' again, the output is right now

assertContains (), assertCount () and assertEmpty () We will learn together with 3 assertion functions that work with arrays.

  • assertContains() : this function is intended whether the value exists or the provided array contains the value we expect.
  • assertCount() : This function expects the number of items in an array of results to match the quantity we want.
  • assertEmpty() : This function expects the resulting array to be empty.

You will get the result of pass 2 test and fail the last test because $princesses not empty.

How to know if writing unittest is satisfactory?

We write a satisfactory unittest when all three of the following are met:

  • Arrange: set the emulation state, initialize the Object, mock Mock
  • Act: Run the correct method we are testing
  • Assert: Compares the expected result with the result returned.

Conclude

Through some of our share, we have also learned the most basic things about UnitTest, the next section I will write about a specific example of writing UnitTest for the CRUD resource functions that we often use. write in the controller. Thank you for reading my shared article

Refer

https://laravel.com/docs/5.8/testing

Share the news now

Source : Viblo