6

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?

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • 1
    Does this answer your question? [What's the difference between unit, functional, acceptance, and integration tests?](https://stackoverflow.com/questions/4904096/whats-the-difference-between-unit-functional-acceptance-and-integration-test) see also https://sqa.stackexchange.com/questions/23392/implementing-the-test-pyramid-as-qa/23396#23396 – N69S Oct 13 '21 at 14:36
  • 2
    Possibly more important than the naming classifications is what they actually do differently. `Tests\TestCase` bootstraps the Laravel application just like it does when you actually use the app. This means you can use the service container for DI, make HTTP requests, or any other thing that depends on Laravel itself. The PHPUnit test case only provides testing helpers, nothing related to Laravel. – Brian Thompson Oct 13 '21 at 14:38
  • @BrianThompson Thanks! That already helps me. So I can also mix both? – Maik Lowrey Oct 13 '21 at 14:42
  • I'm not 100% what you mean by mix both, but the Laravel TestCase will ultimately extend the PHPUnit one. – Brian Thompson Oct 13 '21 at 14:46
  • Ok. I understand. Thank you! – Maik Lowrey Oct 13 '21 at 14:49

1 Answers1

6

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.

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40