Laravel Laravel
  • Prologue

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
    • API Documentation
  • Getting Started

    • Installation
    • Configuration
    • Directory Structure
    • Request Lifecycle
  • Dev Environments

    • Homestead
    • Valet
  • Core Concepts

    • Service Container
    • Service Providers
    • Facades
    • Contracts
  • The HTTP Layer

    • Routing
    • Middleware
    • CSRF Protection
    • Controllers
    • Requests
    • Responses
    • Session
    • Validation
  • Views & Templates

    • Views
    • Blade Templates
    • Localization
  • JavaScript & CSS

    • Getting Started
    • Compiling Assets
  • Security

    • Authentication
    • Authorization
    • Password Reset
    • API Authentication
    • Encryption
    • Hashing
  • General Topics

    • Broadcasting
    • Cache
    • Errors & Logging
    • Events
    • File Storage
    • Mail
    • Notifications
    • Queues
  • Database

    • Getting Started
    • Query Builder
    • Pagination
    • Migrations
    • Seeding
    • Redis
  • Eloquent ORM

    • Getting Started
    • Relationships
    • Collections
    • Mutators
    • Serialization
  • Artisan Console

    • Commands
    • Task Scheduling
  • Testing

    • Getting Started
    • Application Testing
    • Database
    • Mocking
  • Official Packages

    • Cashier
    • Envoy
    • Passport
    • Scout
    • Socialite
  • Appendix

    • Collections
    • Helpers
    • Packages

Testing

  • Introduction
  • Environment
  • Creating & Running Tests

Introduction

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 setup for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.

An ExampleTest.php file is provided in the tests directory. After installing a new Laravel application, simply run phpunit on the command line to run your tests.

Environment

When running tests, Laravel will automatically set the configuration environment to testing. Laravel automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.

You are free to define other testing environment configuration values as necessary. The testing environment variables may be configured in the phpunit.xml file, but make sure to clear your configuration cache using the config:clear Artisan command before running your tests!

Creating & Running Tests

To create a new test case, use the make:test Artisan command:

php artisan make:test UserTest

This command will place a new UserTest class within your tests directory. You may then define test methods as you normally would using PHPUnit. To run your tests, simply execute the phpunit command from your terminal:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

{note} If you define your own setUp method within a test class, be sure to call parent::setUp.

Become a Laravel Partner

Laravel Partners are elite shops providing top-notch Laravel development and consulting. Each of our partners can help you craft a beautiful, well-architected project.

Our Partners
Laravel
Highlights
  • Release Notes
  • Getting Started
  • Routing
  • Blade Templates
  • Authentication
  • Authorization
  • Artisan Console
  • Database
  • Eloquent ORM
  • Testing
Resources
  • Laracasts
  • Laravel News
  • Laracon
  • Laracon EU
  • Laracon AU
  • Jobs
  • Certification
  • Forums
Partners
  • Vehikl
  • Tighten Co.
  • Kirschbaum
  • Byte 5
  • 64Robots
  • Cubet
  • DevSquad
  • Ideil
  • Cyber-Duck
  • ABOUT YOU
  • Become A Partner
Ecosystem
  • Vapor
  • Forge
  • Envoyer
  • Horizon
  • Lumen
  • Nova
  • Echo
  • Valet
  • Mix
  • Spark
  • Cashier
  • Homestead
  • Dusk
  • Passport
  • Scout
  • Socialite
  • Telescope

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in most web projects.

Laravel is a Trademark of Taylor Otwell.
Copyright © 2011-2019 Laravel LLC.

  • Twitter
  • GitHub
  • Discord
Laravel