Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Tests to realize automatic testing in laravel

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Tests to realize automated testing in laravel? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Create a new test

The first step in using PHPUnit is to create a new test class. The convention for 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 files that do not 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 our tests and provides a framework for the test assistant, which we will cover later. ExampleTest.php is a sample test class that contains basic test cases that use the application test assistant-ignore it for now.

To create a new test class, we can create a new file manually 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 like this:

. / tests/...

This will tell the PHPUnit runtime about the tests found in the. / tests/ directory, which, as we knew before, 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.75MbOK (2 tests, 3 assertions)

Now that we have a valid PHPUnit setting, it's time to write a basic test.

Note that it counts 2 tests and 3 assertions because the ExampleTest.php file contains a test with two assertions. Our new basic test includes a separate assertion that has been passed.

Write a basic test

To help with the basic assertions provided by PHPUnit, we will first create a basic class that provides some simple functionality

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report