


What are the different types of tests in Laravel (unit tests, feature tests)?
Jun 22, 2025 am 12:55 AMLaravel provides several types of tests including unit, feature, browser (Dusk), and Pest tests. 1. Unit tests focus on isolated parts like methods or classes, are fast, and don’t involve the full framework. 2. Feature tests simulate HTTP requests to test routes, controllers, and workflows end-to-end. 3. Browser tests via Dusk automate real user interactions for JavaScript-heavy interfaces but are slower. 4. Pest tests offer a concise syntax for Laravel testing, reducing boilerplate while supporting all test types. Each serves a specific purpose, allowing developers to choose based on what they need to test effectively.
Laravel comes with a solid testing foundation out of the box, and understanding the different types of tests it supports helps you write better, more reliable test suites. The main types you’ll work with are unit tests and feature tests, but there are also others like browser tests and Pest tests if you're using Pest PHP.
Unit Tests
Unit tests are for testing small, isolated parts of your application — like individual methods or classes. They don’t involve HTTP requests or interact with the full framework stack. This makes them fast and focused.
- Good candidates for unit tests include helper functions, value objects, or business logic that doesn't depend on Laravel’s full runtime.
- In Laravel, unit tests typically extend
PHPUnit\Framework\TestCase
, notTests\TestCase
, which means they don’t boot the entire Laravel framework before running. - Example: Testing a method that calculates tax based on location without touching the database or routing system.
One thing to note is that many developers skip writing pure unit tests in Laravel because most of their logic tends to be tied into the framework (like controllers or Eloquent models). But when possible, unit tests can speed up your test suite significantly.
Feature Tests
Feature tests are what most Laravel developers use day-to-day. These simulate full HTTP requests and allow you to test how different parts of your app work together — including routes, middleware, controllers, and even database interactions.
- Feature tests extend
Tests\TestCase
and boot the full Laravel application before running. - You can make actual HTTP calls using methods like
$this->get('/url')
,$this->post('/url', $data)
, and chain assertions like->assertStatus(200)
or->assertSee('text')
. - You can test complex workflows like user registration, login, form validation, and authorization checks.
Here's a quick example:
$this->post('/login', [ 'email' => 'test@example.com', 'password' => 'wrong-password' ])->assertRedirect('/');
This type of test gives you confidence that your features behave correctly from the outside-in, just like a real user would experience.
Browser Tests (Dusk)
If you need to test JavaScript-heavy pages or browser interactions, Laravel Dusk provides an expressive API for browser automation using ChromeDriver.
- Dusk tests simulate real user behavior in a browser — clicking buttons, filling forms, checking if elements appear after AJAX calls.
- These tests are slower than feature tests, so it's best to keep their number limited and focus on critical paths like payment flows or complex UI interactions.
- Setup involves installing Laravel Dusk via Composer and running
php artisan dusk:install
.
You might write a Dusk test to confirm that a modal pops up after clicking a button, or that a dropdown menu updates dynamically when you select something.
Pest Tests (if you're using Pest PHP)
Pest is a modern, lightweight testing framework built specifically for Laravel. It works alongside PHPUnit and offers a more concise and expressive syntax.
- Pest tests are mostly used for feature testing but can also handle unit and browser tests.
- It reduces boilerplate code by removing the need for verbose class definitions and repetitive setup.
- For example, instead of creating a class with multiple test methods, Pest lets you define tests using closures inside a simple PHP file.
A basic Pest test looks like this:
test('user can view homepage', function () { $this->get('/')->assertStatus(200); });
It's gaining popularity because it improves readability and developer experience while keeping all the power of Laravel's testing tools.
So depending on what you're trying to test — whether it's a single function, a whole HTTP flow, or complex frontend behavior — Laravel has a test type that fits. Unit and feature tests cover most cases, and tools like Dusk and Pest add flexibility where needed.
The above is the detailed content of What are the different types of tests in Laravel (unit tests, feature tests)?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Steps for unit testing interfaces and abstract classes in Java: Create a test class for the interface. Create a mock class to implement the interface methods. Use the Mockito library to mock interface methods and write test methods. Abstract class creates a test class. Create a subclass of an abstract class. Write test methods to test the correctness of abstract classes.

PHP unit testing tool analysis: PHPUnit: suitable for large projects, provides comprehensive functionality and is easy to install, but may be verbose and slow. PHPUnitWrapper: suitable for small projects, easy to use, optimized for Lumen/Laravel, but has limited functionality, does not provide code coverage analysis, and has limited community support.

In Go function unit testing, there are two main strategies for error handling: 1. Represent the error as a specific value of the error type, which is used to assert the expected value; 2. Use channels to pass errors to the test function, which is suitable for testing concurrent code. In a practical case, the error value strategy is used to ensure that the function returns 0 for negative input.

Unit testing and integration testing are two different types of Go function testing, used to verify the interaction and integration of a single function or multiple functions respectively. Unit tests only test the basic functionality of a specific function, while integration tests test the interaction between multiple functions and integration with other parts of the application.

Performance tests evaluate an application's performance under different loads, while unit tests verify the correctness of a single unit of code. Performance testing focuses on measuring response time and throughput, while unit testing focuses on function output and code coverage. Performance tests simulate real-world environments with high load and concurrency, while unit tests run under low load and serial conditions. The goal of performance testing is to identify performance bottlenecks and optimize the application, while the goal of unit testing is to ensure code correctness and robustness.

Table-driven testing simplifies test case writing in Go unit testing by defining inputs and expected outputs through tables. The syntax includes: 1. Define a slice containing the test case structure; 2. Loop through the slice and compare the results with the expected output. In the actual case, a table-driven test was performed on the function of converting string to uppercase, and gotest was used to run the test and the passing result was printed.

It is crucial to design effective unit test cases, adhering to the following principles: atomic, concise, repeatable and unambiguous. The steps include: determining the code to be tested, identifying test scenarios, creating assertions, and writing test methods. The practical case demonstrates the creation of test cases for the max() function, emphasizing the importance of specific test scenarios and assertions. By following these principles and steps, you can improve code quality and stability.

How to use Gomega for assertions in Golang unit testing In Golang unit testing, Gomega is a popular and powerful assertion library that provides rich assertion methods so that developers can easily verify test results. Install Gomegagoget-ugithub.com/onsi/gomega Using Gomega for assertions Here are some common examples of using Gomega for assertions: 1. Equality assertion import "github.com/onsi/gomega" funcTest_MyFunction(t*testing.T){
