执行所有测试之前如何执行一次命令?

huangapple go评论49阅读模式
英文:

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__.&#39;/../bootstrap/app.php&#39;;

        $app-&gt;make(Illuminate\Contracts\Console\Kernel::class)-&gt;bootstrap();
        
        Artisan::call(&#39;cache:clear&#39;);
        Artisan::call(&#39;route:clear&#39;);
        Artisan::call(&#39;config:clear&#39;);

        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.:

&lt;phpunit 
    ...&gt;
    ...
    &lt;php&gt;
        ...
        &lt;env name=&quot;CACHE_DRIVER&quot; value=&quot;array&quot;/&gt;
        ...
    &lt;/php&gt;
&lt;/phpunit&gt;

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-&gt;call(&#39;cache:clear&#39;);
    $this-&gt;call(&#39;route:clear&#39;);
    $this-&gt;call(&#39;config:clear&#39;);
    $this-&gt;call(&#39;test&#39;);
}

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.

huangapple
  • 本文由 发表于 2023年4月13日 16:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定