我创建了一个 Symfony 命令来将我的应用程序重置为初始状态。要从 cli 运行该命令,我需要输入:
php bin/console app:reset
我想运行那个命令前一次单元测试。我可以做到 每次测试前肯定在之前所有类(class) .因此我使用了该代码:
public function setUp()
{
$kernel = new \AppKernel('test', true);
$kernel->boot();
$app = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$app->setAutoExit(false);
$app->run(new ArrayInput([
'command' => 'app:reset', ['-q']
]), new NullOutput());
}
如上所述,在每次测试之前和使用
setUpBeforeClass()
时效果都很好。我可以在每节课之前都有它,但在所有测试之前一次就足够了,因为该命令需要一些时间才能运行。
最佳答案
Symfony 文档解释了如何做到这一点:How to Customize the Bootstrap Process before Running Tests
总之,需要修改phpunit.xml.dist
调用您自己的 Bootstrap 而不是默认 Bootstrap (并委托(delegate)给默认 Bootstrap )。
关于phpunit - 在所有测试之前运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37653004/