




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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 "%dp0
2、phpunit.phar" %* > phpunit.cmdC:bin> exit6. C:Usersusername> phpunit -version二、简单测试1、初步测试(FirstTest)<?phpclass FirstTest extends PHPUnit_Framework_TestCase public function testArray() $array = array(); $this->assertEquals(0, count($array);测试对象都以classTest命名,继承自PHPUnit_Framework_Te
3、stCase,里面的方法名都已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处测试,一处失败则:<?phpclass FirstTest extends
4、PHPUnit_Framework_TestCase public function testArray() $array = array(); $this->assertEquals(1, count($array);public function testArray2() $array = array(); $this->assertEquals(0, count($array);可以看出共2个测试,2个断言,第一处断言失败,而且第一处失败并没有导致第二处测试受影响。2、依赖测试1(参数依赖)class SecondTest extends PHPUnit_Framework_
5、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(
6、$array);depends testEmpty定义testPush依赖于TestEmpty,testPush依赖于TestEmpty的输出结果当参数传入结果显示2个测试方法,共3个断言,且都成立假如第一个函数返回的数组不为空,<?phpclass SecondTest extends PHPUnit_Framework_TestCase public function testIsEmpty() $array = array();$array = 'not empty' $this->assertEmpty($array); return $array; /*
7、* 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(逻辑依赖)<?phpclass ThirdTest extends PHPUnit_Framework_TestCase publ
8、ic function testOne() $this->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 t
9、estThree定义testFour依赖于testThree函数的断言结果,如果断言失败,testFour不执行4、测试方法传参(数据提供者)方法<?phpclass FourthTest extends PHPUnit_Framework_TestCase /* * dataProvider additionProvider */ public function testAdd($a, $b, $expected) $this->assertEquals($expected, $a + $b); public function additionProvider() return
10、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('f
11、oo', 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.
12、 断言某个对象含有某静态属性assertClassHasStaticAttribute()public function testClassHasStaticAttribute() $this->assertClassHasStaticAttribute('foo', 'stdClass');4. 断言某个数组或者字符串包含某个值或子字符串assertContains()public function testContains() $this->assertContains(4, array(1, 2, 4); public function tes
13、tContains() $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()publ
14、ic 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->
15、;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->a
16、ssertFileExists('/path/to/file'); 10.断言实际值大于期望值(第二个值大于第一个值) assertGreaterThan()public function testGreaterThan() $this->assertGreaterThan(2, 3); 11. 断言实际值大于或等于期望值assertGreaterThanOrEqual()public function testGreaterThanOrEqual() $this->assertGreaterThanOrEqual(2, 3);12.断言实际值小于期望值assert
17、LessThan()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);1
18、5.断言值为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.实际值
19、匹配预期正则表达式 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.日志打印:例如:
20、test.php<?phpclass ParamsTest extends PHPUnit_Framework_TestCasepublic function testCount() $this->assertCount(1, array('foo');public function testCount2() $this->assertCount(0, array();想要打印json格式的日志phpunit -log-json F:json_log.txt test.php"event":"suiteStart",&q
21、uot;suite":"ParamsTest","tests":2"event":"testStart","suite":"ParamsTest","test":"ParamsTest:testCount""event":"test","suite":"ParamsTest","test":"ParamsTest:te
22、stCount","status":"pass","time":0.00054311752319336,"trace":,"message":"","output":"""event":"testStart","suite":"ParamsTest","test":"ParamsTest:testCount2"
23、;"event":"test","suite":"ParamsTest","test":"ParamsTest:testCount2","status":"pass","time":0.00032281875610352,"trace":,"message":"","output":""想要打印xml 格式的日志php
24、unit -log-junit F:xml_log.txt test.php<?xml version="1.0" encoding="UTF-8"?><testsuites> <testsuite name="ParamsTest" file="F:Testtest.php" tests="2" assertions="2" failures="0" errors="0" time="0.001
25、549"> <testcase name="testCount" class="ParamsTest" file="F:Testtest.php" line="4" assertions="1" time="0.000962"/> <testcase name="testCount2" class="ParamsTest" file="F:Testtest.php" line=&qu
26、ot;8" assertions="1" time="0.000587"/> </testsuite></testsuites>2.过滤器操作(1)flter:例如:<?phpclass ParamsTest extends PHPUnit_Framework_TestCasepublic function testA() $this->assertCount(0, array();public function testB() $this->assertCount(1, array('1
27、');public function testC() $this->assertCount(2, array('1','2');我只想要测试testA方法:phpunit -filter 'testA' test.php也可以phpunit -filter 'A' test.php(2).group例如:<?phpclass ParamsTest extends PHPUnit_Framework_TestCase/*group group1*/public function testA() $this->
28、;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');
29、/*group group3*/public function testE() $this->assertCount(2, array('1','2');只测试属于group1分组的方法:phpunit -group group1 test.php可以看到有三处测试3.测试执行参数(1).无断言测试视为风险 -report-useless-tests<?phpclass ParamsTest extends PHPUnit_Framework_TestCasepublic function testA() $this->assertCount(
30、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'<
31、?phprequire './Params.php'class ParamsTest extends PHPUnit_Framework_TestCaseprotected $Params;protected function setUp()$this->Params = new Params();/* * covers Params:A */public function testA() $this->Params->A();$this->Params->B();在打印代码覆盖报告时:(3)意外的输出当做风险 -disallow-test-out
32、putpublic function testA() $this->Params->A();(4)终止代码执行A.首次遇到风险停止执行 -stop-on-risky<?phprequire './Params.php'class ParamsTest extends PHPUnit_Framework_TestCaseprotected $Params;protected function setUp()$this->Params = new Params();public function testA() $this->Params->A(
33、);public function testB() $this->assertTrue(true);public function testC() $this->assertFalse(false);遇到风险但不停止执行但是加上参数-stop-on-risky可以看到在第一次遇到风险时便停止运行B. 首次出现失败后停止执行 -stop-on-failure<?phpclass ParamsTest extends PHPUnit_Framework_TestCasepublic function testA()$this->assertTrue(false);publi
34、c 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()$
35、this->assertTrue(true);可以看到测试进行了10次(6)输出调试信息 -debug(7)加载配置文件 -configuration, -c(配置文件为phpunit.xml 或 phpunit.xml.dist,若在当前目录,即使不加参数配置文件也会自动加载,关于配置文件,请自行查阅资料)(8) 忽略当前工作目录下的配置文件 -no-configuration4.代码覆盖率(1)首先要开启php中 xdebug扩展,而且必须以zend_extension的方式引入:(2)最后以一个Company类的增删改查实例来说明代码覆盖率并作为本次文
36、档的结束:原Company类:<?phpclass Companypublic $member = array('xiaoming','xiaohong','xiaoli');public function add($name)if($name != '')$this->member = $name;public function del($name)if($name != '' && ($key = array_search($name,$this->member) != fal
37、se)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;测试类:<?phprequire './Company
38、.php'class CompanyTest extends PHPUnit_Framework_TestCaseprotected $company;protected function setUp()$this->company = new Company();/*dataProvider addProvider*/public function testAdd($name)$this->company->add($name);$this->assertEquals($name,$this->company->membercount($this-
39、>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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小儿药品备货管理制度
- 河道垃圾转运方案(3篇)
- 装修异形改造方案(3篇)
- 冷饮工厂安全管理制度
- 单位规范采购管理制度
- 公共信息安全管理制度
- 清淤运输管理方案(3篇)
- 墙面漏水修缮方案(3篇)
- 工程检查公司管理制度
- 医院服务接待管理制度
- 道观庙宇托管协议书
- 硬膜下血肿护理查房
- 2025年四川省成都市武侯区中考道德与法治模拟试卷
- 2025年中国天然云母市场调查研究报告
- 2025年市政工程地下管网试题及答案
- 关爱眼健康远离近视眼科普呵护眼睛让视界更精彩课件
- 地球是人类共同的家园课件-地理商务星球版(2024)七年级下册
- 【课件】跨学科实践:探索厨房中的物态变化问题(教学课件)初中物理人教版(2024)八年级上册
- PHPstorm激活码2025年5月13日亲测有效
- 区块链与供应链管理的完美结合实现高效项目融资
- 2022年高考地理试卷(天津)(解析卷)
评论
0/150
提交评论