您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit基境-setUp
发布时间:2021-09-19 18:01:43编辑:雪饮阅读()
像下面这样一个phpunit测试类:StackTest.php:
use PHPUnit\Framework\TestCase;
final class StackTest extends TestCase
{
private $stack;
public function testEmpty(): void
{
$this->stack=[];
$this->assertTrue(empty($this->stack));
}
public function testPush(): void
{
$this->stack=[];
array_push($this->stack, 'foo');
$this->assertSame('foo', $this->stack[count($this->stack)-1]);
$this->assertFalse(empty($this->stack));
}
public function testPop(): void
{
array_push($this->stack, 'foo');
$this->assertSame('foo', array_pop($this->stack));
$this->assertTrue(empty($this->stack));
}
}
testMethod1()
修改的属性状态
无法传递给 testMethod2()
使用。
再次运行就没有问题了:
use PHPUnit\Framework\TestCase;
final class StackTest extends TestCase
{
private $stack;
protected function setUp(): void
{
$this->stack = [];
}
public function testEmpty(): void
{
$this->assertTrue(empty($this->stack));
}
public function testPush(): void
{
array_push($this->stack, 'foo');
$this->assertSame('foo', $this->stack[count($this->stack)-1]);
$this->assertFalse(empty($this->stack));
}
public function testPop(): void
{
array_push($this->stack, 'foo');
$this->assertSame('foo', array_pop($this->stack));
$this->assertTrue(empty($this->stack));
}
}
关键字词:phpunit,setUp
上一篇:redis乐观锁解决超卖超买问题
下一篇:phpunit类方法运行生命周期
相关文章
- phpunit使用testdox的testdox-text与testdox-html参数
- phpunit敏捷文档testdox的带参情况
- phpunit使用testdox情况下多个测试方法的名字互相之间
- phpunit中filter的使用(匹配命名空间、类、方法以及数
- phpunit中expectException的使用
- phpunit中NOTICE、WARNING、ERROR的断言支持
- phpunit中expectDeprecationMessageMatches的使用
- phpunit中expectDeprecationMessage的使用
- phpunit中的expectDeprecation的使用
- phpunit使用弱比较(assertEquals)时在差异生成过程中