Create a Laravel Unit Test in 45 Seconds
45sQuick, actionable tutorial hook showing how to create a unit test from scratch.
▶ Play ClipThis tutorial demonstrates how to create and run a unit test in Laravel using PHPUnit. It covers checking for PHPUnit installation, creating a test file with Artisan, writing a test for a Customer model's full name method, and running the test.
Welcome to a Laravel unit test tutorial using PHPUnit.
Open composer.json to verify phpunit/phpunit is listed.
Customer model has a fullName() method that concatenates first and last name.
Run 'php artisan make:test CustomerTest --unit' to generate the test file.
CustomerTest extends TestCase from PHPUnit; write test method with docblock description.
Instantiate Customer model directly (no factory), set first and last name, then use assertEquals to check fullName() returns 'John Smith'.
Use 'php artisan test' to run all tests or 'php artisan test --filter=CustomerTest' for a specific test.
Unit testing in Laravel is straightforward with PHPUnit. By creating a test file, writing assertions, and running tests via Artisan, you can ensure your model methods work correctly.
"Title accurately describes a Laravel unit test tutorial."
What command creates a unit test file in Laravel?
php artisan make:test CustomerTest --unit
00:44
What does the --unit flag do in the make:test command?
It specifies that the test is a unit test and places the file under the unit test directory.
01:21
How do you instantiate a model in a unit test without using factories?
Use the new operator directly, e.g., $customer = new Customer();
02:37
What method is used to assert that two values are equal in PHPUnit?
assertEquals
02:45
How do you run only a specific test file using Artisan?
php artisan test --filter=CustomerTest
03:54
Creating a Unit Test with Artisan
Shows the exact command to generate a unit test file, a fundamental step in Laravel testing.
00:44Direct Instantiation in Unit Tests
Clarifies that unit tests should not use factories but directly instantiate models.
02:37Running Tests with Filter
Demonstrates how to run a single test file efficiently using the --filter option.
03:24[00:00] welcome to another develop and design
[00:02] tutorial in this video we will explore
[00:05] how to create a unit test in laravel
[00:07] using PHP unit first we need to check
[00:10] that we have the PHP unit package
[00:12] installed by opening up the Project's
[00:14] composer. Json file the package name we
[00:17] should see is phpunit phpunit now we
[00:20] know this is installed I am going to
[00:22] open up the customer model class to
[00:24] write a function for our unit test this
[00:27] is just a simple function called full
[00:29] name which returns a string
[00:30] concatenating the customer's first name
[00:32] and last name to create our unit test we
[00:36] need to open our terminal or command
[00:38] prompt then navigate to our laravel
[00:41] project directory Now to create the
[00:44] customer unit test file itself we need
[00:47] to type the command PHP artisan make
[00:50] colon test customer test-- unit then hit
[00:55] enter now the command has run
[00:57] successfully we should now see the file
[01:00] path to our unit test displayed in the
[01:02] terminal just a quick breakdown of the
[01:04] command itself PHP artisan make colon
[01:07] test creates the test file customer test
[01:10] in Pascal case names the test file for
[01:13] the customer class's unit tests only the
[01:15] customer class's unit tests will go in
[01:17] this file-- unit specifies that the
[01:21] tests are unit tests and will place the
[01:23] file under the unit test directory now
[01:26] if we navigate to the test directory in
[01:28] our code editor then into the unit
[01:30] directory we should see our customer
[01:32] test file let's click on it to open it
[01:35] we can tell the test is a PHP unit test
[01:38] as the customer test class extends the
[01:40] test case class from the PHP unit
[01:42] library now to write the test we'll
[01:44] first add a description in the doc block
[01:46] above the test function explaining what
[01:48] it
[01:49] covers let's put Test the full name
[01:52] method of the customer model Returns the
[01:54] correct
[01:56] format it's good to put clear
[01:58] descriptions as it helps other
[02:00] developers understand the tests now
[02:02] let's give the test function a name
[02:04] let's call it Test full name Returns the
[02:06] correct
[02:08] format now let's delete everything that
[02:11] was pre-generated inside the example
[02:13] function then begin writing the test by
[02:16] creating a variable called customer
[02:19] which will be an instance of the
[02:21] customer model
[02:23] class we need to give the customer a
[02:25] first name and last name to test the
[02:28] function I'll assign the first name John
[02:31] and the last name Smith to the customer
[02:34] for unit tests we do not use model
[02:37] Factory calls instead we instantiate the
[02:40] class using the new
[02:42] operator now let's add a call to a
[02:45] method called assert
[02:47] equals this method takes two variables
[02:50] and asserts whether they are equal or
[02:52] not if they are equal then the unit test
[02:55] has passed if they are not equal then it
[02:58] has failed
[03:01] for this method's first parameter we
[03:03] want to put the string value we expect
[03:05] the customer full name method to return
[03:08] which is John
[03:10] Smith the second parameter we want to
[03:12] pass is the full name method call from
[03:14] our customer variable as this should
[03:17] concatenate the customer's first name
[03:19] and last name to equal John
[03:21] Smith now to run the test we want to
[03:24] open up the terminal and ensure we are
[03:26] in our laravel project
[03:28] directory then type the command PHP
[03:31] Artisan test and hit enter Then in the
[03:34] terminal we can see that our unit test
[03:36] has passed successfully the command we
[03:39] just ran on its own runs all the
[03:41] tests if we have lots of tests it could
[03:44] take a while to run them all if we just
[03:46] want to see the outcome of one test file
[03:48] then we can run the command with a
[03:50] filter let's try the filter command in
[03:52] the
[03:54] terminal type PHP Artisan test-- filter
[03:58] equals customer test
[04:00] where customer test is the name of the
[04:02] test then hit enter to run it and there
[04:07] we have the customer test which has run
[04:09] successfully if you have any questions
[04:11] about this then please leave a comment
[04:13] below and I will get back to you as soon
[04:15] as I
[04:16] can if you found this video useful then
[04:19] please give it a like and hit the
[04:20] Subscribe button
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.