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 ...
随机推荐
- [开发笔记]-Linq to xml学习笔记
最近需要用到操作xml文档的方法,学习了一下linq to xml,特此记录. 测试代码: class Program { //参考: LINQ to XML 编程基础 - luckdv - 博客园 ...
- Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; ...
- FormWindowState.Maximized的陷阱
MDI父窗体下面有很多子窗体.子窗体打开时都是以FormWindowState.Maximized的属性打开的,但是结果部分如意,部分奇怪. 先贴上单实例的代码: static TestForm _s ...
- drupal模板命名规则
1.block--[region[module--delta]].tpl.php基于主题文件: block.tpl.php block--module--delta.tpl.phpblock--mod ...
- 在android中配置 slf4j + log4j 日志记录框架
需求: 在项目开发中,需要记录 操作日志 .起初自己写了个简单的日志记录文本写入到文本的方法,后来随着项目的膨胀,需要考虑更多的操作,开始考虑性能问题. 实现: 考虑使用 slf4j + log4j ...
- 利用SpringCloud搭建一个最简单的微服务框架
http://blog.csdn.net/caicongyang/article/details/52974406 1.微服务 微服务主要包含服务注册,服务发现,服务路由,服务配置,服务熔断,服务降级 ...
- static为什么一般与final一起用?
static和final的意义是不同的,static修饰的时候代表对象是静态的,而final修饰的时候代表对象只能赋值一次,他们连用的时候是因为定义的那个对象既要它是静态的,也要求它的值不能再被修改. ...
- jpa无外键配置
在用jpa这种orm框架时,有时我们实体对象存在关联关系,但实际的业务场景可能不需要用jpa来控制数据库创建数据表之间的关联约束,这时我们就需要消除掉数据库表与表之间的外键关联.但jpa在处理建立外键 ...
- Sql Server性能优化辅助指标 - SET STATISTICS TIME ON和SET STATISTICS IO ON
1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. ), ...
- 让你的MyEclipse像Visual Studio 2008一样拥有强大功能智能感知功能
Windows→Preferences→Java→Editor→Content Assist 我们看到其中的AutoActivation Delay默认值为200(单位是毫秒)也就是说在打“.”之后停 ...