您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertIsScalar
发布时间:2021-10-04 11:29:38编辑:雪饮阅读()
assertIsScalar断言主要是断言一个实际值的数据类型是否是一个标量scalar。
ScalarTest.php:
<?php
use PHPUnit\Framework\TestCase;
class ScalarTest extends TestCase
{
public function testFailure()
{
$this->assertIsScalar(null);
}
public function testOne()
{
$a=new StdClass;
$this->assertIsScalar($a);
}
public function testTwo()
{
$this->assertIsScalar(1);
}
public function testThree()
{
$a=1;
$this->assertIsScalar($a);
}
}
use PHPUnit\Framework\TestCase;
class ScalarTest extends TestCase
{
public function testFailure()
{
$this->assertIsScalar(null);
}
public function testOne()
{
$a=new StdClass;
$this->assertIsScalar($a);
}
public function testTwo()
{
$this->assertIsScalar(1);
}
public function testThree()
{
$a=1;
$this->assertIsScalar($a);
}
}
运行结果如:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\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\ScalarTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
FF.. 4 / 4 (100%)
Time: 00:00.013, Memory: 6.00 MB
There were 2 failures:
1) ScalarTest::testFailure
Failed asserting that null is of type "scalar".
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ScalarTest.php:8
2) ScalarTest::testOne
Failed asserting that stdClass Object &000000006e1b47040000000069dd14f6 () is of type "scalar".
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ScalarTest.php:13
FAILURES!
Tests: 4, Assertions: 4, Failures: 2.
这第一个是null不算是标量,第二个是stdcalss的实例算是对象,也不算标量,后面两个分别是字面量1可以算是标量,而变量a的值也是1也就算是标量。
关键字词:phpunit,assertIsScalar