您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit多个depends(多重依赖)
发布时间:2021-09-12 22:00:25编辑:雪饮阅读()
有时候一个方法可能依赖自多个生产者(其它方法),此时用多个depends标记,并且该方法中的参数也是多个,并且每个参数按自身的顺序和这多个depends一一对应。
实例如:
MultipleDependenciesTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MultipleDependenciesTest extends TestCase
{
public function testProducerFirst(): string
{
$this->assertTrue(true);
return 'first';
}
/**
* @depends testProducerFirst
* @depends testProducerSecond
*/
public function testConsumer(string $a, string $b): void
{
$this->assertSame('first', $a);
$this->assertSame('second', $b);
}
public function testProducerSecond(): string
{
$this->assertTrue(true);
return 'second';
}
}
use PHPUnit\Framework\TestCase;
final class MultipleDependenciesTest extends TestCase
{
public function testProducerFirst(): string
{
$this->assertTrue(true);
return 'first';
}
/**
* @depends testProducerFirst
* @depends testProducerSecond
*/
public function testConsumer(string $a, string $b): void
{
$this->assertSame('first', $a);
$this->assertSame('second', $b);
}
public function testProducerSecond(): string
{
$this->assertTrue(true);
return 'second';
}
}
运行效果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar C:\Users\Administrator\PhpstormProjects\untitled\MultipleDependenciesTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:00.005, Memory: 20.00 MB
OK (3 tests, 4 assertions)
那么官方文档有一点说明:
测试可以使用多个 @depends 标注。PHPUnit 不会更改测试的运行顺序,因此你需要自行保证某个测试所依赖的所有测试均出现于这个测试之前。
但是我这里在语法上将testProducerSecond这个被testConsumer依赖的方法放在了testConsumer之后,并没有什么问题。
那么我觉得可能有其它什么因素或者说的应该不是语法顺序之类吧。
关键字词:phpunit,depends