What is the difference between PHPUnit\Framework\TestCase and Tests\TestCase?

Viewed 323

I noticed that in the Example Tests, the two classes are built in.

Feature Test => use Tests\TestCase;

Unit Test => PHPUnit\Framework\TestCase;

What are the differences between the both? In which cases do you use one or the other?

1 Answers

PHPUnit\Framework\TestCase

This is the standard out-of-the-box parent for PHPUnit tests. It only has the functionality provided by PHPUnit.

It is best used when you want "true" unit tests that have no knowledge or scaffolding of Laravel. They are also much faster.

Tests\TestCase

This is a parent class for many kinds of tests (can be unit, integration, or feature) that depend on the Laravel app to function. These scenarios might include:

  • Using Eloquent factories to create models
  • Using Facades and their fakes
  • Unit testing components that use global helpers (e.g. config())
  • Using database transactions and/or resets for each test
  • Using any of the other augmented functionality and assertions offered by Laravel

PHPUnit\Framework\TestCase is the parent of Tests\TestCase, so all of PHPUnit's standard functionality is also available.

You can use both! I use PHPUnit's TestCase when possible for unit tests, and Laravel's for integration and feature testing.

Related