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

Time:10-14

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?

CodePudding user response:

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

Tests\TestCase is an ancestor of PHPUnit\Framework\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