Kei*_*vis 5 php phpunit unit-testing
我的测试应该失败,但它通过了:
public function test_getValidProviderCodes(){
$aTest = PRIDE\Reflection::executeStaticMethodForClassName(Apps_DoctorsReports::class, "getValidProviderCodes");
print_r($aTest);
$this->assertContains("xxxxxxxxxxxxxx", $aTest);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Testing started at 8:53 AM ...
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.
Configuration read from C:\inetpub\Intranet_Local\phpunit\phpunit.xml
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => MAYER
[4] => MAY00
[5] => MAYERIC
[6] => COH00
[7] => COH01
[8] => POWELL
[9] => POW00
[10] => JOHN00
[11] => FINO
[12] => POL01
[13] => NONAP
[14] => RAYE00
[15] => HOPS00
[16] => CHAH00
)
-
Time: 1.24 seconds, Memory: 8.25Mb
OK (1 test, 1 assertion)
Run Code Online (Sandbox Code Playgroud)
该值"xxxxxxxxxxxxxx"
显然不在该数组中。我已经使用这个函数数百次了,但从未见过这种行为。
(如果我更改$aTest
为[]
,测试就会失败。)
这是另一个测试运行:
public function test_getValidProviderCodes(){
$aTest = PRIDE\Reflection::executeStaticMethodForClassName(Apps_DoctorsReports::class, "getValidProviderCodes");
$this->assertContains("S01", implode(", ", $aTest));
}
Run Code Online (Sandbox Code Playgroud)
输出:
Testing started at 9:04 AM ...
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.
Configuration read from C:\inetpub\Intranet_Local\phpunit\phpunit.xml
Failed asserting that '0, 1, 2, M01, M03, M04, M05, N02, C01, C02, C03, C04, P01, P02, P03, P04, P05, P06, P07, R01, H01, J01, J02' contains "S01".
C:\inetpub\Intranet_Local\phpunit\library\classes\apps\DoctorsReportsTest.php:61
Time: 1.54 seconds, Memory: 8.75Mb
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Run Code Online (Sandbox Code Playgroud)
还可以assertContains
检查对象身份(默认行为)。因此,要跳过此操作,您需要向该方法传递一个可选标志。这个问题已经在 github 上介绍过了。
因此,要使测试失败,只需将最后一个可选参数的checkForNonObjectIdentity
值传递true
如下:
public function test_getValidProviderCodes(){
$aTest =
array(
0,1,2,'MAYER'
);
print_r($aTest);
$this->assertContains(
"xxxxxxxxxxxxxx",
$aTest,
'message',
false,true,true);
}
Run Code Online (Sandbox Code Playgroud)
输出:
无法断言数组包含“xxxxxxxxxxxxxxx”。
希望这有帮助