What is UnitTest? Things to know about Unit Test in Laravel – PHP

Tram Ho

overview

Hello everyone, surely every programmer we can not ignore testing. Testing is an indispensable job in the software development process. Initially, when I entered programming, I just thought that checking the program’s accuracy or detecting unexpected errors was only for QA / Tester, but developers only need to focus on writing code. to run smoothly. But my thought is completely wrong when I started working and exposed to a methodical project, when testing will be parallel to our programming as well as by ourselves although there is a way. slightly different from the QA or Tester test.

Writing tests is very important as soon as we start working on the project, because only when we control every small element of our code can we develop applications, control errors. the easy way and that time will be much shorter than the code without being tested. Simply test for each line of our own code to write, when there is an error, we will know and localize the error to fix it to avoid spending too much time on finding errors instead of building and developing application. use. Having a bug fixes it, not waiting for QA)

1. Main problem

So what is Unit Test? why programmers are required to learn and apply it in their projects.

Units are translated as units. Ie testing at the unit level, but each unit here is Methods, functions, classes. Writing tests for these functions, classes helps us to understand the processing flow of each small component so that we can safely build larger functions from them.

  • Unit Test helps us to save time and money in application development.
  • Unit Test makes it easy to capture the flow of code in the code and from there it can be easily upgraded.
  • Unit Test quality can be as a document for the project.
  • Through Unit Test we can separate individual functions that are both easy for testing and easy to reuse.

Some tools for using Unit Test :

  • Junit : Junit is a free testing tool for the Java language.
  • NUnit : NUnit is widely used in Unit Testing and with all .net languages. It is an open source tool that allows to write scripts by hand. It also supports testing against data that can be run in parallel.
  • EMMA : EMMA is an open source toolkit for analyzing and reporting code written in the Java language. Emma supports coverage types such as method, line, and basic block. It is Java based so it is independent of external libraries and can access source code.
  • PHPUnit : PHPUnit is a unit testing tool for PHP programmers. PHPUnit uses small components to test and test them separately. This tool also allows developers to use predefined validation methods to assert that the system works in a certain way as it is working properly or not.

UnitTest will have the main states are:

  • Fail: Error when testing
  • Ignore: Pause execution
  • Pass: Test failed and works as expected.

A good Unit Test needs to meet the following conditions:

  • Each UnitTest must be completely independent and unrelated to the other components. In the event of a change to another component, the UnitTest will not be affected.
  • Test only one unit at a time.
  • When upgrading or modifying or writing new components be sure to write UnitTest for them.
  • The test will not affect the real database.

2. UnitTest in Laravel.

Today, we will also learn about PHPUnit which is the main tool for writing and running UnitTest which is built into Laravel.

In laravel, to write tests we will write in test folder

Explanation of directories:

  • tests: contains code for testing the application
  • tests / Browser: not used to test the view, interacting with the browser.
  • tests / Feature: contains feature test files
  • tests / Unit: contains unit test files
  • TestCase: This is a bootstrap file that sets up the Laravel environment for tests
  • phpunit.xml is the configuration file for PHPUnit

Rules for creating test files:

  • The name of the test class will have the suffix structure added from Test. eg: PostControllerTest
  • Create the test file path with the same structure as the real file path: For example, I want to test a Controller and have the folder path: Http / Controllers / PostController.php, then we will create a unit test file with the path structure is: tests / Unit / Http / Controllers / PostControllerTest.php.

Things we need to test in Laravel:

  • Controllers
  • Models
  • View
  • Repository
  • Auth policies …

To create the unit test file:

When creating a test file we will use two methods setUp and tearDown. So what are those two methods used for? Simply put, the setUp method will be a place for us to create a pre-test environment such as creating a user with admin rights, or using a pre-test model. This method will run before each of our test functions.

So you will also guess what tearDown to do right. It is the method for us to clean up the environments set up during setUp.

Assertions

Assertions: Translated is affirmative. That is, the assertions method will help us confirm if the output is exactly what we want.

Some common assertions include:

  • assertEquals : This function will perform if the 2 results are equal
  • assertInstanceOf : This function will perform to determine whether the comparison result is correct with the desired intance or not.
  • assertArrayHasKey : Performs whether the result to be compared (array) inside has a given key or not.
  • assertTrue : Performs whether the result to be compared is true or not.
  • assertFalse : Performs whether the result to be compared is true or not.
  • assertIsArray : Performs to see if the result to be compared is an array or not.
  • And there are some other assertes everyone in the laravel doc to learn more.

Example testing a model in laravel:

In the test file:

Finally we will run the artisan command:

Above I have tested for a Model User with relation with Role. In this test file, some functions like getFillable () or getHidden () are used to test the fillable or hidden properties in Laravel. In addition to test relation, I used assertInstanceOf to test whether the relationship in the user is an instance of BelongsTo Laravel or not, and also test OwnerKeyName or ForeignKeyName as above.

summary

Through this article I have shared some basic knowledge about UnitTest. With this knowledge working, we need to learn more about the methods available to test in the main documentation of each tool. Parallel testing when coding is a very important and necessary thing to ensure professional application development and save effort and time. Hope the article can help everyone a little bit in their work. Thank you and see you on a nearest date!

Share the news now

Source : Viblo