英文:
How to execute a command once before all tests?
问题
我正在寻找在开始所有测试之前清除Laravel缓存的解决方案(请注意,我指的是所有测试,而不是每个单独的测试):
$this->artisan('cache:clear');
$this->artisan('route:clear');
$this->artisan('config:clear');
附注:Laravel 9 和 PHPUnit 9.5
英文:
I'm looking a solution to clear cache in Laravel, before starting all the tests (please note I mean ALL tests, not every single test):
$this->artisan('cache:clear');
$this->artisan('route:clear');
$this->artisan('config:clear');
P.S. Laravel 9 and PHPUnit 9.5
答案1
得分: 0
在 tests/TestCase.php 中添加以下内容:
protected function setUp(): void
{
parent::setUp();
$this->artisan('cache:clear');
$this->artisan('route:clear');
$this->artisan('config:clear');
}
英文:
Add
protected function setUp(): void
{
parent::setUp();
$this->artisan('cache:clear');
$this->artisan('route:clear');
$this->artisan('config:clear');
}
in tests/TestCase.php
答案2
得分: 0
以下是翻译好的部分:
你可以通过创建自己的TestCase(或编辑原始的TestCase.php
),并将这些任务添加到createApplication
方法中:
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
Artisan::call('cache:clear');
Artisan::call('route:clear');
Artisan::call('config:clear');
return $app;
}
}
每个测试都应该扩展这个TestCase,而不是Illuminate\Foundation\Testing\TestCase
。
你还可以考虑在开发环境中不缓存路由和配置。在你的phpunit.xml文件中,你也可以将测试期间使用的缓存驱动程序设置为与开发期间使用的不同驱动程序,例如:
<phpunit
...
...
<php>
...
<env name="CACHE_DRIVER" value="array"/>
...
</php>
</phpunit>
这将消除在运行测试之前清除缓存的必要性。
在这个答案中,你可以了解更多关于测试期间配置缓存的信息。在那里,你还可以找到一个链接,该链接讨论了在运行测试之前清除缓存的GitHub问题。
英文:
You can do this by creating your own TestCase (or edit the original TestCase,php
), and add these tasks to the createApplication
method:
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
Artisan::call('cache:clear');
Artisan::call('route:clear');
Artisan::call('config:clear');
return $app;
}
}
Each test should extend this TestCase instead of Illuminate\Foundation\Testing\TestCase
You can also consider not to cache routes and configuration in your development environment. In your phpunit.xml you can also set the cache driver that is used during testing to a different one than that is used during development, e.g.:
<phpunit
...>
...
<php>
...
<env name="CACHE_DRIVER" value="array"/>
...
</php>
</phpunit>
That would drop the necessity to clear the caches before running your tests.
In this answer you can read more about configuration caching during testing. There you can also find a link to a GitHub issue that talks about clearing caches before running tests.
答案3
得分: 0
As I known, no such way to do this, because this is not the range of unit tests.
Unit tests designed to setting same environment for each tests, environment of all tests is not its responsibility.
But you can still work around by make a custom command to call config:clear
and test
.
1. Make a custom command.
php artisan make:command test
2. Edit your test command.
Default path will be app/Console/Commands/test.php
.
...
/**
* Execute the console command.
*/
public function handle()
{
$this->call('cache:clear');
$this->call('route:clear');
$this->call('config:clear');
$this->call('test');
}
3. Call unit tests by your own test command.
If you didn't change the default $signature
, app:test
will be the default command you just created.
Run php artisan app:test
to call unit tests.
英文:
As I known, no such way to do this, because this is not the range of unit tests.
Unit tests designed to setting same environment for each tests, environment of all tests is not its responsibility.
But you can still work around by make a custom command to call config:clear
and test
.
1. Make a custom command.
php artisan make:command test
2. Edit your test command.
Default path will be app/Console/Commands/test.php
.
...
/**
* Execute the console command.
*/
public function handle()
{
$this->call('cache:clear');
$this->call('route:clear');
$this->call('config:clear');
$this->call('test');
}
3. Call unit tests by your own test command.
If you didn't change the default $signature
, app:test
will be the default command you just created.
Run php artisan app:test
to call unit tests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论