




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PhPUnit学习教程 一、phpUnit安装1. 为 PHP 的二进制可执行文件建立一个目录 如:C:bin;2. 将;C:bin附加到PATH环境变量中;3. 将php.exe所在目录也加到环境变量中去如 ;D:Webxamppphp ,保证你写的php文件能在各个目录下运行;4.下载https:/phar.phpunit.de/phpunit-old.phar到C:binphpunit.phar;5. 安装C:Usersusername cd C:binC:bin echo php %dp0phpunit.phar %* phpunit.cmdC:bin exit6. C:Usersusername phpunit -version二、简单测试1、初步测试(FirstTest)assertEquals(0, count($array);测试对象都以classTest命名,继承自PHPUnit_Framework_TestCase,里面的方法名都已testFunction命名, assertEquals断言0等于count($array); 保存文件到F:/Test文件加下,test.php, cmd切到F:/Test下,输入phpunit test.php上面显示用时0秒,内存3M,测试通过,共1个测试,1个断言;如果故意写错,$this-assertEquals(1, count($array),此时数组是空,断言不成立:可以看到有1处失败,在第七行,testArray函数中断言与结果不匹配,后面也报错了如果有2处测试,一处失败则:assertEquals(1, count($array);public function testArray2() $array = array(); $this-assertEquals(0, count($array);可以看出共2个测试,2个断言,第一处断言失败,而且第一处失败并没有导致第二处测试受影响。2、依赖测试1(参数依赖)class SecondTest extends PHPUnit_Framework_TestCase public function testIsEmpty() $array = array(); $this-assertEmpty($array); return $array; /* * depends testIsEmpty */ public function testAdd(array $array) array_push($array, phpunit); $this-assertEquals(phpunit, $array count($array)-1); $this-assertNotEmpty($array);depends testEmpty定义testPush依赖于TestEmpty,testPush依赖于TestEmpty的输出结果当参数传入结果显示2个测试方法,共3个断言,且都成立假如第一个函数返回的数组不为空,assertEmpty($array); return $array; /* * depends testIsEmpty */ public function testAdd(array $array) array_push($array, phpunit); $this-assertEquals(phpunit, $array count($array)-1); $this-assertNotEmpty($array);结果为1测试1断言,1断言失败,跳过1个测试3、依赖测试2(逻辑依赖)assertTrue(FALSE); /* * depends testOne */ public function testTwo() public function testThree()$array = array();$this-assertNotEmpty($array); /* * depends testThree */ public function testFour() depends testOne定义testTwo依赖于TestOne函数的断言结果,如果断言失败,testTwo不执行depends testThree定义testFour依赖于testThree函数的断言结果,如果断言失败,testFour不执行4、测试方法传参(数据提供者)方法assertEquals($expected, $a + $b); public function additionProvider() return array( array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 3) ); dataProvider表明为testAdd方法提供参数的方法是additionProvider, 需要返回的2维数组,第二维数组值的位置,对应测试方法参数的位置,参数个数和数组长度要相等。可以看出共测试了4遍,3遍正确,最后一遍失败三、phpunit断言详解1.断言数组含有某键assertArrayHasKey()public function testArrayHasKey() $this-assertArrayHasKey(foo, array(bar = baz),this array not exit the key);2.断言某个对象含有某属性assertClassHasAttribute()class Apublic $aa;class AssertTest extends PHPUnit_Framework_TestCasepublic function testClassHasAttribute() $this-assertClassHasAttribute(aa, A); 3. 断言某个对象含有某静态属性assertClassHasStaticAttribute()public function testClassHasStaticAttribute() $this-assertClassHasStaticAttribute(foo, stdClass);4. 断言某个数组或者字符串包含某个值或子字符串assertContains()public function testContains() $this-assertContains(4, array(1, 2, 4); public function testContains() $this-assertContains(aa,aab); 以上2个结果都为真5.断言元素数量assertCount()public function testCount() $this-assertCount(0, array(foo);6.断言某个值为空assertEmpty()public function testEmpty() $this-assertEmpty(array();7.断言2个值相等(可用于数字,字符串,数组,对象,XML文档)assertEquals()public function testEquals() $this-assertEquals(1, 0);public function testEquals() $this-assertEquals(array(a, b, c), array(a, b, c); public function testEquals() $expected = new stdClass; $expected-foo = foo; $expected-bar = bar; $actual = new stdClass; $actual-foo = foo; $actual-bars = bar; $this-assertEquals($expected, $actual); 8.断言为假assertFalse()public function testFalse() $this-assertFalse(TRUE); 9.断言文件存在assertFileExists()public function testFileExists() $this-assertFileExists(/path/to/file); 10.断言实际值大于期望值(第二个值大于第一个值) assertGreaterThan()public function testGreaterThan() $this-assertGreaterThan(2, 3); 11. 断言实际值大于或等于期望值assertGreaterThanOrEqual()public function testGreaterThanOrEqual() $this-assertGreaterThanOrEqual(2, 3);12.断言实际值小于期望值assertLessThan()public function testLessThan() $this-assertLessThan(3, 2); 13.断言实际值小于或等于期望值 assertLessThanOrEqual()public function testFailure() $this-assertLessThanOrEqual(1, 1); 14. 断言结果是某种类型 assertInternalType()public function testInternalType() $this-assertInternalType(int, 42);15.断言值为null assertNull() 空字符串也不成立public function testNull() $this-assertNull(null); 16.断言某个实例存在某属性 assertObjectHasAttribute()class Apublic $aa;class AssertTest extends PHPUnit_Framework_TestCasepublic function testObjectHasAttribute() $this-assertObjectHasAttribute(aa, new A);17.实际值匹配预期正则表达式 assertRegExp() public function testFailure() $this-assertRegExp(/d+/, 1256899); 18.断言2个值全等assertSame()public function testFailure() $this-assertSame(2204, 2204);19.断言为真 assertTrue()public function testTrue() $this-assertTrue(true); 四、参数详解1.日志打印:例如:test.phpassertCount(1, array(foo);public function testCount2() $this-assertCount(0, array();想要打印json格式的日志phpunit -log-json F:json_log.txt test.phpevent:suiteStart,suite:ParamsTest,tests:2event:testStart,suite:ParamsTest,test:ParamsTest:testCountevent:test,suite:ParamsTest,test:ParamsTest:testCount,status:pass,time:0.00054311752319336,trace:,message:,output:event:testStart,suite:ParamsTest,test:ParamsTest:testCount2event:test,suite:ParamsTest,test:ParamsTest:testCount2,status:pass,time:0.00032281875610352,trace:,message:,output:想要打印xml 格式的日志phpunit -log-junit F:xml_log.txt test.php 2.过滤器操作(1)flter:例如:assertCount(0, array();public function testB() $this-assertCount(1, array(1);public function testC() $this-assertCount(2, array(1,2);我只想要测试testA方法:phpunit -filter testA test.php也可以phpunit -filter A test.php(2).group例如:assertCount(0, array();/*group group1*/public function testB() $this-assertCount(1, array(1);/*group group1*/public function testC() $this-assertCount(2, array(1,2);/*group group2*/public function testD() $this-assertCount(2, array(1,2);/*group group3*/public function testE() $this-assertCount(2, array(1,2);只测试属于group1分组的方法:phpunit -group group1 test.php可以看到有三处测试3.测试执行参数(1).无断言测试视为风险 -report-useless-testsassertCount(0, array();public function testB() $this-assertCount(1, array(1);public function testC() 第三个方法没有断言,被提示风险(2).意外的代码覆盖称为风险 -strict-coverage 需在打印覆盖报告时生效(covers 标注来指明测试方法想要对哪些方法进行测试)例如:?phpclass Paramspublic function A()echo A;public function B()echo B;Params = new Params();/* * covers Params:A */public function testA() $this-Params-A();$this-Params-B();在打印代码覆盖报告时:(3)意外的输出当做风险 -disallow-test-outputpublic function testA() $this-Params-A();(4)终止代码执行A.首次遇到风险停止执行 -stop-on-riskyParams = new Params();public function testA() $this-Params-A();public function testB() $this-assertTrue(true);public function testC() $this-assertFalse(false);遇到风险但不停止执行但是加上参数-stop-on-risky可以看到在第一次遇到风险时便停止运行B. 首次出现失败后停止执行 -stop-on-failureassertTrue(false);public function testB()$this-assertTrue(true);很明显第一个断言失败;假如不加参数 可以看到2个测试都执行了,第一个失败加上参数 -stop-on-failure 以后:可以看到当第一个测试失败时停止执行。C.首次遇到错误停止执行 -stop-on-error(5)让测试进行多次执行 -repeat 次数class ParamsTest extends PHPUnit_Framework_TestCasepublic function testA()$this-assertTrue(false);public function testB()$this-assertTrue(true);可以看到测试进行了10次(6)输出调试信息 -debug(7)加载配置文件 -configuration,-c(配置文件为phpunit.xml或phpunit.xml.dist,若在当前目录,即使不加参数配置文件也会自动加载,关于配置文件,请自行查阅资料)(8) 忽略当前工作目录下的配置文件 -no-configuration4.代码覆盖率(1)首先要开启php中 xdebug扩展,而且必须以zend_extension的方式引入:(2)最后以一个Company类的增删改查实例来说明代码覆盖率并作为本次文档的结束:原Company类:member = $name;public function del($name)if($name != & ($key = array_search($name,$this-member) != false)unset($this-member$key);public function edit($name1,$name2)if($name1 != & $name2 != & ($key = array_search($name1,$this-member) != false)$this-member$key = $name2;public function select()return $this-member;测试类:company = new Company();/*dataProvider addProvider*/public function testAdd($name)$this-company-add($name);$this-assertEquals($name,$this-company-membercount($this-company-member)-1);public function addProvider()return array(array(nameA),);/*dataProvider delProvider*/public function testDel($name)$this-company-del($name);$this-assertNotContains($name,$this-company-member);public function delProvider()return array(array(xiaoming),array(xiaoli),array(xiaohong);/*dataProvider editProvider*/public function testEdit($name1,$name2)$this-company-edit($name1,$name2);$this-assertNotContains($name1,$this-company-member);$this-assertContains($name2,$this-company-member);public function editProvider()return array(array(xiaoming,mingxiao),array(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 菏泽市中石油2025秋招面试半结构化模拟题及答案炼油工艺技术岗
- 铜陵市中石油2025秋招笔试模拟题含答案油气储运与管道岗
- 国家能源阿拉善盟2025秋招面试专业追问及参考计算机与自动化岗位
- 2025年湖南招聘考试试题及答案
- 新乡市中石化2025秋招面试半结构化模拟题及答案新材料与新能源岗
- 中国联通武威市2025秋招行业解决方案岗位专业追问清单及参考回答
- 铜陵市中石油2025秋招面试半结构化模拟题及答案油田工程技术岗
- 国家能源海南藏族自治州2025秋招面试专业追问及参考财务审计岗位
- 国家能源湖州市2025秋招笔试模拟题及答案
- 2025年超市课长考试试题及答案
- SMS安全管理体系培训课件
- 电子商务运营推广数据化分析模板
- 北京外汇交易知识培训课件
- 喷漆技师基础知识培训课件
- 一级实验室生物安全手册
- 学堂在线 中国建筑史-史前至两宋辽金 章节测试答案
- 2025年党员党的基本理论应知应会知识100题及答案
- 评估“蛇吞象”式海外并购模式的绩效与影响
- 【公开课】+地球的运动-地球的公转+课件-2024-2025学年七年级地理上学期人教版
- 国家保密培训课件
- 2025至2030年中国牛油果行业市场发展前景及投资规模预测报告
评论
0/150
提交评论