Introduction to the PHPUnit introductory course of Laravel test (with examples)

01-28-2024

The content of this article is about the introduction of PHPUnit tutorial for Laravel test (with examples), which has certain reference value. Friends who need it can refer to it and hope it will help you. PHPUnit is one of the oldest and most famous PHP unit test packages. It is mainly used for unit testing, which means that you can test the code with as small components as possible, but it is also very flexible and can be used for many more than unit testing.

PHPUnit contains many simple and flexible assertions that allow you to easily test code, and these assertions are very effective when you test specific components. However, it does mean that testing more advanced code, such as controller and form submission validation, can be much more complicated.

In order to help developers to develop more easily, Laravel framework contains a series of application testing helpers, which allow you to write very simple PHPUnit tests to test complex parts of an application.

The purpose of this tutorial is to introduce you to the basics of PHPUnit testing, using the default PHPUnit assertion and Laravel test assistant. The purpose of this is that at the end of this tutorial, you can confidently write basic tests for your application.

prerequisite

This tutorial assumes that you are familiar with Laravel and know how to run commands (such as php artisan commands) in the application directory. We will create several basic sample classes to learn how different testing tools work, so we suggest that you create a new application for this tutorial.

If Laravel is already installed, you can create a new test application by running the following command:

laravel new phpunit-tests

Alternatively, you can create a new application directly using Composer:

composer create-project laravel/laravel --prefer-dist

Other installation methods can also be found in Laravel documentation.

Create a new test

The first step in using PHPUnit is to create a new test class. The convention of test classes is that they are stored under. /tests/ in the application directory. In this folder, each test class is named Test.php. This format allows PHPUnit to find each test class-it will ignore any file that doesn't end in Test.php.

In the new Laravel application, you will notice that there are two files in the. /tests/ directory: ExampleTest.php and TestCase.php. The TestCase.php file is a boot file used to set up the Laravel environment in our tests. This allows us to use Laravel Facades in testing and provides a framework for test assistants, which we will introduce later. ExampleTest.php is a sample test class that contains basic test cases for using the application test assistant-ignore it for the time being.

To create a new test class, we can manually create a new file or run the Artisan command make:test provided by Laravel.

To create a test class named BasicTest, we just need to run this artisan command:

php artisan make:test BasicTest

Laravel will create a basic test class as follows:

./tests/ ...

This will tell the tests found in the. /tests/ directory by the PHPUnit runtime. As we know before, this is the convention for storing tests.

Now that we have created a basic test and know the PHPUnit configuration, it is time to run the test for the first time.

You can run the test by running the following phpunit command:

./vendor/bin/phpunit

You should see output similar to this:

PHPUnit 4.8.19 by Sebastian Bergmann and contributors. .. Time: 103 ms, Memory: 12.75Mb OK (2 tests, 3 assertions)

Now that we have an effective PHPUnit setting, it's time to start writing a basic test.

Note that it will count 2 tests and 3 assertions, because the ExampleTest.php file contains a test with two assertions. Our new basic test includes a single assertion that passed.

Writing a Basic Test To help the basic assertions provided by PHPUnit, we will first create a basic class that provides some simple functions.

Create a new file named Box.php in the. /app/ directory and copy this sample class:

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us