There is a lot of information within this file, however, the most important section, for now, is the testsuite directory definition: This is where you configure the specific options for your tests. PHPUnit will automatically look for a file named phpunit.xml or in the current directory when it is run. If you forget the test prefix, then PHPUnit will ignore the method.īefore we run our test suite for the first time, it is worth pointing out the default phpunit.xml file that Laravel provides. Like the Test suffix for class names, this test prefix tells PHPUnit what methods to run when testing. The most important thing to notice here is the test prefix on the method name. Laravel will create a basic test class that looks like this: get('/') In order to create a test class called BasicTest, we just need to run this artisan command: $ cd phpunit-test To create a new test class, we can either create a new file manually or run the helpful Artisan make:test command provided by Laravel. This allows us to use Laravel facades in tests and provides the framework for the testing helpers, which we will look at shortly. TestCase.php: The TestCase.php file is a bootstrap file for setting up the Laravel environment within our tests.Feature/ExampleTest.php and Unit/ExampleTest.php : The ExampleTest.php is an example test class that includes a basic test case using the application testing helpers – ignore it for now.In a new Laravel application, you will notice two files in the. This format allows PHPUnit to find each test class-it will ignore anything that does not end in Test.php. Inside this folder, each test class is named as Test.php. The convention for test classes is that they are stored within. The first step when using PHPUnit is to create a new test class. Other installation options can also be found in the Laravel documentation. If you have the Laravel installer set up, you can create a new test application by running: $ laravel new phpunit-testsĪlternatively, you can create a new application by using Composer directly: $ composer create-project laravel/laravel -prefer-dist phpunit-tests We will be creating a couple of basic example classes to learn how the different testing tools work, and as such, it is recommended that you create a fresh application for this tutorial. This tutorial assumes that you are already familiar with Laravel and know how to run commands within the application directory (such as php artisan commands). The aim is for you to be confident in writing basic tests for your applications by the end of the tutorial. The purpose of this tutorial is to introduce you to the basics of PHPUnit testing, using both the default PHPUnit assertions and the Laravel test helpers. To help make things easier for developers, the Laravel PHP framework includes a collection of application test helpers, which allow you to write very simple PHPUnit tests to test complex parts of your application. It does mean, however, that testing more advanced code such as controllers and form submission validation can be a lot more complicated. PHPUnit includes a lot of simple and flexible assertions that allow you to easily test your code, which works really well when you are testing specific components. It is primarily designed for unit testing, which means testing your code in the smallest components possible, but it is also incredibly flexible and can be used for a lot more than just unit testing. PHPUnit is one of the oldest and most well-known unit testing packages for PHP.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |