To automatically consider a base TestCase class in all your test files in Laravel, you can leverage PHPUnit's configuration file (phpunit.xml). By setting a default bootstrap file in your phpunit.xml, you can ensure that your custom TestCase class is always included.
Here’s how you can do it:
-
Create a
TestCaseclass: Ensure you have a baseTestCaseclass in yourtestsdirectory. This is typically located attests/TestCase.php.<?php namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; } -
Modify
phpunit.xml: Open yourphpunit.xmlfile located at the root of your Laravel project. Add or modify thebootstrapattribute to point to yourTestCase.phpfile.<phpunit bootstrap="tests/TestCase.php"> <!-- other configurations --> </phpunit>
By setting the bootstrap attribute, PHPUnit will automatically include your TestCase class before running any tests. This way, you don't need to manually include TestCase in each test file.
Here’s an example of what your phpunit.xml might look like:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/TestCase.php"
colors="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
</phpunit>
With this setup, PHPUnit will automatically consider your TestCase class for all test files, and you won't need to manually include it in each test file.