Unit Testing In Laravel 1 - Beginner Guide

Photo by Sigmund on Unsplash

Unit Testing In Laravel 1 - Beginner Guide

Table of contents

No heading

No headings in the article.

What is Unit Testing?

Unit testing is a software testing method where “units" i.e the individual components of software are tested.

Why Unit Testing?

  • Any bugs are found easily and quicker
  • Good unit tests serve as project documentation
  • Unit testing is an Integral part of extreme programming
  • Unit testing reduces code complexity

Unit Testing In Laravel

Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box and a phpunit.xml file is already set up for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.

By default, Laravel application's has tests directory which contains two directories: Feature and Unit.

Unit directory contains unit tests; unit tests are tests that focus on a very small, isolated portion of your code. Most unit tests probably focus on a single method. Tests written in "Unit" test directory do not boot your Laravel application and therefore are unable to access your application's database or other framework services.

Feature directory contains feature tests; feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint. Generally, most of your tests should be feature tests. These types of tests provide the most confidence that your system as a whole is functioning as intended.

Resources:

Testing: Getting Started

The importance of unit testing, or how bugs found in time will save you money