Adding a New Test File

Adding a new test file

Adding a new test file

It's time to add a simple file to your Laravel project.

In this tutorial you will:

  • Generate a New Test Using Artisan
  • See Your New Test Pass
  • Review Every Line of The File for Understanding
  • Modify the Test, and Finally Write Some Code
  • Add your New Test to your Github Repository

Generate a New Test Using Artisan

You can use artisan's make:test command to create a new test file for you and populate it with a starting test.

First, take a look at the help documentation for this artisan command to ensure you are aware of how to use it.

sail artisan help make:test 

The usage is straightforward, you simply need to establish the name for your test.

Naming Your Test File

In order for the test to be picked up by the system, the file needs to end in Test.

The name should be Studly Cased. That means it should contain no spaces, and each word should start with a capital letter.

The name should be also be concise and meaningful.

Your goal is to test the Widget Index Route, so WidgetIndexRouteTest should work nicely.

sail artisan help make:test WidgetIndexRouteTest

That's it, your new file has been created.

Composer Dump

In order for phpunit to find any new test files, you will need to run composer dump. It is a good idea to get into the habit of running this right away after generating a new test.

composer dump