php单元测试断言方法
1.assertArrayHasKey()
用法:$this->assertArrayHasKey('foo', ['bar' => 'baz']);
等同于array_key_exists(),查数组有木有某个键
2.assertClassHasAttribute()
用法:$this->assertClassHasAttribute('platform', User::class);
查看某个类是否有某个变量
3.assertArraySubset()
用法:$this->assertArraySubset(['config' => ['key-a', 'key-b']], ['config' => ['key-a']]);//false
$this->assertClassHasAttribute(['config' => ['key-a']], ['config' => ['key-a', 'key-b']);//true
传入两个数组,判断第二个数组是否包含第一个数组
4.assertClassHasStaticAttribute()
用法:$this->assertClassHasStaticAttribute('foo', User::class);//false
$this->assertClassHasStaticAttribute('platform', User::class);//true
查看某个类是否有某个静态变量
发现大概都挺简单的。。。
后面简单记录下吧
$this->assertContains(4, [1, 2, 3]);//判断数组是否含有某个数字元素
$this->assertContains('foo', 'FooBar');//判断字符串是否含有某个字符串
$this->assertContains('foo', 'FooBar', '', true);//判断字符串是否含有某个字符串(大小写不敏感)
assertContainsOnly();//类似$this->assertArrayHasKey
$this->assertContainsOnlyInstancesOf(
Foo::class,
[new Foo, new Bar, new Foo]
);//判断类集合是否包含Foo::class
$this->assertCount(0, ['foo']);//判断数组长度是否为传入参数
$this->assertDirectoryExists('/path/to/directory');//判断指定的目录是否存在
$this->assertDirectoryIsReadable('/path/to/directory');//判断指定的目录是否存在和是否可读
$this->assertDirectoryIsWritable('/path/to/directory');//判断指定的目录是否存在和是否可写
$this->assertEmpty(['foo']);//判断是否为空
$this->assertNotEmpty(['foo']);//判断是否为空
$this->assertEqualXMLStructure($expected, $actual);//判断xml结构
$this->assertEquals('bar', 'baz');//判断是否相等(数字,字符串,数组,对象)
$this->assertEquals(1.0, 1.1, '', 0.2);//细化到float $this->assertFalse(true);//判断是否为false
$this->assertFileEquals('/home/sb/expected', '/home/sb/actual');//判断指定的文件是否相同
$this->assertFileExists('/path/to/file');//判断文件是否存在
$this->assertFileIsReadable('/path/to/file');//判断文件是否可读
$this->assertFileIsWritable('/path/to/file');//判断文件是否可写
$this->assertGreaterThan(2, 1);//当第2个的值 不大于 第1个的值时报告错误
$this->assertGreaterThanOrEqual(2, 1);//当第2个参数的值 不大于且不等于 第1个参数的值时报告错误
$this->assertInfinite(1);//判断int
$this->assertInstanceOf(RuntimeException::class, new Exception);//当参数二不是参数一的实例时报错
$this->assertInternalType('string', 42);//判断参数2类型是否正确
$this->assertIsReadable('/path/to/unreadable');//文件或目录不可读时报告错误
$this->assertIsWritable('/path/to/unwritable');//文件或目录不可写入时报告错误
$this->assertJsonFileEqualsJsonFile( 'path/to/fixture/file', 'path/to/actual/file');//文件或目录的值不匹配时报告错误
$this->assertJsonStringEqualsJsonFile( 'path/to/fixture/file', json_encode(['Mascot' => 'ux']) );//json或文件或目录的值不匹配时报告错误
$this->assertJsonStringEqualsJsonString( json_encode(['Mascot' => 'Tux']), json_encode(['Mascot' => 'ux']) );//两个json的值不匹配时报告错误
$this->assertLessThan(1, 2);//当参数2的值不小于参数1的值时报告错误
$this->assertLessThanOrEqual(1, 2);//当参数2的值不小于且不等于参数1的值时报告错误
$this->assertNan(1);//不是 NAN 时报告错误
$this->assertNull('foo');//不是 null 时报告错误
$this->assertObjectHasAttribute('foo', new stdClass);//new stdClass为$object当 $object->attributeName 不存在时报告错误
$this->assertRegExp('/foo/', 'bar');//不匹配于正则表达式时报告错误
$this->assertStringMatchesFormat('%i', 'foo');//不匹配于定义的格式时报告错误
$this->assertStringMatchesFormatFile('/path/to/expected.txt', 'foo');//不匹配于内容所定义的格式时报告错误
$this->assertSame('2204', 2204);//当两个变量的值与类型不完全相同时报告错误
$this->assertSame(new stdClass, new stdClass);//当两个变量不是指向同一个对象的引用时报告错误
$this->assertStringEndsWith('suffix', 'foo');//当参数2不以参数1结尾时报告错误
$this->assertStringEqualsFile('/home/sb/expected', 'actual');//当参数1(路径)所指定的文件其内容不是参数2时报告错误
$this->assertStringStartsWith('prefix', 'foo');//当参数2不以参数1开头时报告错误
$this->assertThat( $theBiscuit, $this->logicalNot( $this->equalTo($myBiscuit) ) );//当 $value(参数1) 不符合约束条件 $constraint(参数2) 时报告错误
表 A.1. 约束条件
| 约束条件 | 含义 |
|---|---|
PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName) |
此约束将另外一个约束应用于某个类或对象的某个属性。 |
PHPUnit_Framework_Constraint_IsAnything anything() |
此约束接受任意输入值。 |
PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key) |
此约束断言所评定的数组拥有指定键名。 |
PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value) |
此约束断言所评定的 array 或实现了 Iterator 接口的对象包含有给定值。 |
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnly(string $type) |
此约束断言所评定的 array 或实现了 Iterator 接口的对象仅包含给定类型的值。 |
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnlyInstancesOf(string $classname) |
此约束断言所评定的 array 或实现了 Iterator 接口的对象仅包含给定类名的类的实例。 |
PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10) |
此约束检验一个值是否等于另外一个。 |
PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10) |
此约束检验一个值是否等于某个类或对象的某个属性。 |
PHPUnit_Framework_Constraint_DirectoryExists directoryExists() |
此约束检验所评定的目录是否存在。 |
PHPUnit_Framework_Constraint_FileExists fileExists() |
此约束检验所评定的文件名对应的文件是否存在。 |
PHPUnit_Framework_Constraint_IsReadable isReadable() |
此约束检验所评定的文件名对应的文件是否可读。 |
PHPUnit_Framework_Constraint_IsWritable isWritable() |
此约束检验所评定的文件名对应的文件是否可写。 |
PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value) |
此约束断言所评定的值大于给定值。 |
PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value) |
此约束断言所评定的值大于或等于给定值。 |
PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName) |
此约束断言所评定的类具有给定属性。 |
PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName) |
此约束断言所评定的类具有给定静态属性。 |
PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName) |
此约束断言所评定的对象具有给定属性。 |
PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value) |
此约束断言所评定的值与另外一个值全等。 |
PHPUnit_Framework_Constraint_IsFalse isFalse() |
此约束断言所评定的值为 false。 |
PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className) |
此约束断言所评定的对象是给定类的实例。 |
PHPUnit_Framework_Constraint_IsNull isNull() |
此约束断言所评定的值为 null。 |
PHPUnit_Framework_Constraint_IsTrue isTrue() |
此约束断言所评定的值为 true。 |
PHPUnit_Framework_Constraint_IsType isType(string $type) |
此约束断言所评定的值是指定类型的。 |
PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value) |
此约束断言所评定的值小于给定值。 |
PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value) |
此约束断言所评定的值小于或等于给定值。 |
logicalAnd() |
逻辑与(AND)。 |
logicalNot(PHPUnit_Framework_Constraint $constraint) |
逻辑非(NOT)。 |
logicalOr() |
逻辑或(OR)。 |
logicalXor() |
逻辑异或(XOR)。 |
PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern) |
此约束断言所评定的字符串匹配于正则表达式。 |
PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case) |
此约束断言所评定的字符串包含指定字符串。 |
PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix) |
此约束断言所评定的字符串以给定后缀结尾。 |
PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix) |
此约束断言所评定的字符串以给定前缀开头。 |
$this->assertTrue(false);//当 参数 为 false 时报告错误
$this->assertXmlFileEqualsXmlFile('/home/sb/expected.xml', '/home/sb/actual.xml');//当 参数2 对应的 XML 文档与 参数1 对应的 XML 文档不相同时报告错误
$this->assertXmlStringEqualsXmlFile('/home/sb/expected.xml', '<foo><baz/></foo>');//当 参数2 对应的 XML 文档与 参数1 对应的 XML 文档不相同时报告错误
$this->assertXmlStringEqualsXmlString('<foo><bar/></foo>', '<foo><baz/></foo>');//当 参数2 对应的 XML 文档与 参数1 对应的 XML 文档不相同时报告错误
官方文档:https://phpunit.de/manual/current/zh_cn/appendixes.assertions.html#appendixes.assertions.assertArrayHasKey
php单元测试断言方法的更多相关文章
- unittest 单元测试框架断言方法
unittest单元测试框架的TestCase类下,测试结果断言方法:Assertion methods 方法 检查 版本 assertEqual(a, b) a == b assertNotEqu ...
- Selenium实战(四)——unittest单元测试2(断言方法+discover()多测试用例的执行)
一.断言方法 方法 检查 版本 assertEqual(a,b) a==b assertNotEqual(a,b) a!=b assertTrue(x) bool(x) is True a ...
- unittest常用的断言方法
unittest常用的断言方法 #msg:判断不成立时需要反馈的字符串 assertEqual(self, first, second, msg=None) --判断两个参数相等:first == s ...
- 测试教程网.unittest教程.7. 各种断言方法
From: http://www.testclass.net/pyunit/assert/ 背景 unittest支持各种断言方法. 断言列表 官方文档 方法 检查点 assertEqual(a, b ...
- Robotium_断言方法assert、is、search
下面的这些方法都主要用来判断测试结果是否与预期结果相符,一般把is和search方法放在assert里面判断.assert最常用的还是assertThat方法,是Junit的判断,这里就不多说了.断言 ...
- Selenium 2自动化测试实战28(断言方法)
一.断言方法 执行用例的过程中,最终用例是否执行通过,是通过判断测试得到的实例结果与预期结果是否相等决定的.unittest框架的TestCase类提供下面这些方法用于测试结果的判断. -assert ...
- (三)unittest断言方法的介绍
断言如同在测试用例上,类似于预期结果与实际结果是否一致,如果一致则表示测试通过,Assert断言很好的用于测试结果判断上,更灵活的对预期结果和实际结果进行对比,下面简单的介绍一下unittest的As ...
- unittest框架里的常用断言方法:用于检查数据
1.unittest框架里的常用断言方法:用于检查数据. (1)assertEqual(x,y) 检查两个参数类型相同并且值相等.(2)assertTrue(x) 检查唯一的参数值等于True(3)a ...
- Visaul Studio2015安装以及c++单元测试使用方法
Visual Studio 2015安装流程 vs2015是一款十分好用的IDE,接下来就介绍一下安装流程.这里采用在线安装方式,从官网下载使得安装更加安全. 第一步:在百度中搜索Visual ...
随机推荐
- Fine-tuning Convolutional Neural Networks for Biomedical Image Analysis: Actively and Incrementally如何使用尽可能少的标注数据来训练一个效果有潜力的分类器
作者:AI研习社链接:https://www.zhihu.com/question/57523080/answer/236301363来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- an error has occurred while trying to access the log file. logging may not function properly
I had this issure a couple of days ago when open my vs2012 on windows8,by google i find the soluth ...
- 正则表达式的使用(C#)
1, C#中与正则表达式相关类. C#中与正则表达式相关类的几个常用类是Regex,Match,Group,Captrue,RegexOption首先我们看看这几个类的类图关系,如何你有正则表达式基础 ...
- UI设计 - 手机列表风格总结
1 概述 1.1 背景 对UI设计过程中常见的列表风格进行总结.希望对后续的设计工作有所帮助 1.2 预期读者 UI设计师,用户体验设计师,项目经理,美工 1.3 ...
- 如何在ROS中使用PCL—数据格式(1)
在ROS中点云的数据类型 在ROS中表示点云的数据结构有: sensor_msgs::PointCloud sensor_msgs::PointCloud2 pcl::PointCl ...
- ubuntu16.04安装workbench
sudo dpkg -i mysql-workbench-community-6.3.10-1ubuntu16.04-amd64.deb 报错: Selecting previously unsele ...
- 记录第一次使用jni编译so包的入门操作
1.配置 下载自己相对应的ndk平台版本后配置到studio 在local.properties加入路径 在gradle.properties文件添加 2.创建工具类(注意方法都是native的) 3 ...
- r语言 function 指定多个返回值
# Goals: To write functions # To write functions that send back multiple objects. # FIRST LEARN ABOU ...
- 安卓程序代写 网上程序代写[原]BluetoothAdapter解析
这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...
- (笔记)Mysql命令mysqladmin:修改用户密码
mysqladmin命令用于修改用户密码. mysqladmin命令格式:mysqladmin -u 用户名 -p 旧密码 password 新密码 1) 给root加个密码ab12首先在DOS下进入 ...