PHPUnit | assertNotContains()函数
assertNotContains ()函数是 PHPUnit 中的内置函数,用于断言一个没有值的数组。如果数组不包含提供的值,则此断言将返回 true,否则返回 false,如果为 true,则断言的测试用例通过,否则测试用例失败。
语法:
assertNotContains(mixed $value, array $array, string $message = ''])
参数:此函数接受三个参数,如上述语法所示。参数说明如下:
- $value :该参数表示不包含在数组中的值。
- $array :这个参数是一个数组,断言函数将检查它是否包含值。
- $message :此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。
下面的程序说明了 assertNotContains()函数:
程序一:
"value ba", "b" =>"value b");
$value = "value ba";
// assert function to test whether 'value' is a value of array
$this->assertNotContains($value, $testArray, "testArray contains value as value") ;
}
}
?>
输出:
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 21 ms, Memory: 4.00MB
There was 1 failure:
1) GeeksPhpunitTestCase::testNegativeTestcaseForAssertNotContains
testArray contains value as value
Failed asserting that an array does not contain 'value ba'.
/home/shivam/Documents/geeks/phpunit/abc.php:11
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
方案二:
"value ba", "b" =>"value b");
$value = "value b";
// assert function to test whether 'value' is a value of array
$this->assertNotContains($value, $testArray, "testArray contains value as value") ;
}
}
?>
输出:
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 21 ms, Memory: 4.00MB
OK (1 test, 1 assertion)
注意:要使用 phpunit 运行测试用例,请按照此处的步骤操作。