Tuesday, November 24, 2026SubscribeMastheadAbout
Cali ReporterCalifornia · National · West Coast · Good News

Advertisement

Technology

Laravel Testing Strategy: Feature vs Unit Tests

Laravel supports three types of tests—Feature, Unit, and Policy—each with a specific scope. Feature tests are used for HTTP flows and database interactions, while Unit tests focus on pure business logic. Testing…

Portrait of Benjamin Boyce
Artificial Intelligence & Technology · November 20, 2026
Share

AI-generated editorial illustration from an imagined camera angle.
AI-generated editorial illustration from an imagined camera angle. Source image: dev.to.

Laravel supports three kinds of tests, each with a specific scope: Feature Tests, Unit Tests, and Policy Tests. Feature tests are used to test the full HTTP request/response cycle, making real database calls, going through middleware, and running validation. They are used for any code that touches HTTP. Unit tests, on the other hand, test a single class in isolation, with no database or HTTP interaction. They are fast and used for Services, Actions, and pure business logic. Policy tests are a subset of Feature or Unit tests, but they are worth calling out as every Policy deserves its own test. The ratio across these projects is roughly 80% Feature tests and 20% Unit tests (1). [1]

Feature tests are structured using the AAA pattern – Arrange, Act, Assert – and each test has exactly one Act, which is the HTTP call or method call. Everything before is setup and everything after is assertions. An example of a Feature test is the CreateOrderTest, which tests the HTTP flow for placing an order. It includes setup, the HTTP call, and assertions for the HTTP response, database state, and side effects like events and mail (1). [1]

Unit tests extend PHPUnit\Framework\TestCase and are fast because they do not involve the framework boot, database, or HTTP. They are used for pure business logic and are typically used for testing Services, Actions, and DTOs. An example of a Unit test is the OrderServiceTest, which tests the calculation of total with tax and the creation of a payment intent. Unit tests use mocks and fakes to isolate the test from external services (1). [1]

Fakes are used to replace external services in tests to prevent tests from hitting real external services. Laravel provides fakes for common external services such as Mail, Events, Jobs, and HTTP. For example, Mail::fake() is used to fake email sending, and Event::fake() is used to fake event dispatching. Using fakes ensures that tests are isolated and do not depend on external services (1). [1]

Advertisement

The source specifies that Laravel testing includes three main types of tests: Feature tests, Unit tests, and Policy tests. Feature tests are used for HTTP flows and database interactions, covering the full request/response cycle. These tests involve real database calls, middleware, validation, and controllers. Unit tests, on the other hand, isolate a single class, typically used for pure business logic such as Services and Actions. Policy tests are a subset of Feature or Unit tests, but they specifically test authorization rules. The text also mentions that the test structure for Feature tests is located in the directory `tests/Feature/Orders/CreateOrderTest.php`, which includes methods like `test_authenticated_user_can_place_an_order`. This test uses the `RefreshDatabase` trait to reset the database before each test, ensuring isolation and consistency. The example provided uses the AAA pattern (Arrange, Act, Assert) to structure the test logic, with one Act per test representing the HTTP call or method call. [1]

The source describes the use of factories for creating test data in Laravel. Factories are preferred over manual database inserts because they respect model logic and are more readable. For example, `User::factory()->create()` is used to create a user, and `Product::factory()->outOfStock()->create()` is used to create a product with zero stock. This approach supports defining states for common test scenarios, such as `outOfStock`, `inStock`, and `featured`, which are defined in the `database/factories/ProductFactory.php` file. The source also highlights that tests should never manually insert data using `DB::insert()` or seeders, as these methods are brittle and do not respect model logic. Using factories ensures that test data is created consistently and is aligned with the application's model definitions. [1]

The source outlines the use of Laravel's Fakes to isolate tests from external services. For example, `Mail::fake()` is used to prevent real emails from being sent during tests, and `Event::fake()` is used to prevent real events from being dispatched. These fakes allow developers to assert that specific emails, events, or jobs are sent or dispatched without relying on external services. The source provides examples of how to use these fakes, such as `Mail::assertSent(OrderConfirmationMail::class)` to verify that a specific email was sent, and `Event::assertDispatched(OrderPlaced::class)` to verify that an event was dispatched. The use of Fakes is emphasized as a best practice, as it ensures that tests are not affected by real external services, making them more reliable and faster to execute. [1]

The source specifies that Laravel's Unit tests are located in the `tests/Unit/Services/OrderServiceTest.php` file and extend `PHPUnit\Framework\TestCase` rather than Laravel’s `TestCase`. This means that Unit tests do not involve the framework boot, database, or HTTP, making them faster and more isolated. The example provided tests the `OrderService` class, which includes methods like `calculateTotal()` and `initiatePayment()`. These tests use mocking via `Mockery` to simulate external dependencies, such as the `StripePaymentServiceInterface`. The source also highlights that Unit tests are typically used for pure business logic, such as Services and Actions, and are not used for HTTP flows or database interactions. This distinction ensures that Unit tests remain fast and focused on isolated logic, while Feature tests handle the broader application flow. [1]

Advertisement

The source provides an example of how to test Laravel Policies, which are used to enforce authorization rules. The test class `OrderPolicyTest` includes methods like `test_user_can_view_their_own_order` and `test_user_cannot_view_another_users_order`, which verify that users can only view their own orders and that admins can view any order. These tests use the `RefreshDatabase` trait to ensure that the database is reset before each test. The source emphasizes that Policy tests are critical for security, as they ensure that authorization logic is correctly implemented. It also mentions that every Policy should have its own test class, as authorization rules are security-critical and require thorough testing. This approach ensures that all authorization logic is verified independently and is not overlooked in the testing process. [1]

The source includes information on how to efficiently run Laravel tests. It recommends using the `php artisan test --compact` command to run all tests in a compact format, and the `--parallel` flag to run tests in parallel on multi-core machines, which significantly improves performance in CI environments. The source also provides examples of how to run specific tests, such as using `php artisan test --compact tests/Feature/Orders/CreateOrderTest.php` to run a specific test file, or `php artisan test --compact --filter=test_authenticated_user_can_place_an_order` to run a specific test method. These commands allow developers to run only the tests that are relevant to their current work, improving feedback speed and reducing test execution time. The source also notes that in CI, the `--parallel` flag should always be used to maximize efficiency and speed up test execution. [1]

Sources

Dev.to

Prepared with AI assistance from the cited sources.

The Cali Reporter newsletter

The stories worth your time.

California, the country, and what comes next. Join our free newsletter for reporting that keeps you informed.

By joining, you agree to receive the Cali Reporter newsletter. Unsubscribe at any time. Privacy policy.

More in Technology

  • Modern Web Guidance 1.0 and Browser Compatibility

    Modern Web Guidance 1.0, announced by Chrome for Developers on November 10, introduces browser compatibility data to assist coding agents in building web features. The initiative emphasizes the importance of fallback…

  • Apache mod_http2 Vulnerability Detection with Nmap

    A new Nmap scripting module has been developed to detect the Apache mod_http2 vulnerability (CVE-2026-119618), addressing the challenges of identifying affected systems accurately.

  • Securing Remote Vendor Access in Industrial Networks

    Best practices for managing remote vendor access in operational technology (OT) networks emphasize controlled access points, multifactor authentication, and time-bound sessions to reduce cybersecurity risks.

Advertisement