您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertContains
发布时间:2021-10-01 16:19:24编辑:雪饮阅读()
assertContains断言用来断言一个值是否在一个堆中,例如一个值是否在一个数组中。
若存在于该数组中则断言成功,否则断言失败。
ContainsTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class ContainsTest extends TestCase
{
public function testFailure(): void
{
$this->assertContains(4, [1, 2, 3]);
}
public function testSuccess(): void
{
$this->assertContains(4, [1, 2, 3,4]);
}
}
use PHPUnit\Framework\TestCase;
final class ContainsTest extends TestCase
{
public function testFailure(): void
{
$this->assertContains(4, [1, 2, 3]);
}
public function testSuccess(): void
{
$this->assertContains(4, [1, 2, 3,4]);
}
}
运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ContainsTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 00:00.010, Memory: 4.00 MB
There was 1 failure:
1) ContainsTest::testFailure
Failed asserting that an array contains 4.
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ContainsTest.php:8
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
第一个断言4,自然不在数组[1,2,3]中,于是断言失败。
第二个断言4,自然在数组[1,2,3,4]中,于是断言成功。
关键字词:phpunit,assertContains