使用PHPUnit进行单元测试并生成代码覆盖率报告的方法
php  /  管理员 发布于 4年前   588
安装PHPUnit 使用 使用Composer构建你的项目 我们将新建一个 创建项目结构 使用Composer构建工程 到此我们就完成项目框架的构建,下面开始写业务和测试用例。 编写测试用例 创建文件 创建相应的测试文件 执行单元测试 这是一个非常简单的测试用例类,可以看到,执行了共3个测试用例,共3个断言,共1个失败,可以参照 代码覆盖率 代码覆盖率反应的是 测试覆盖率的检测对象是我们的业务代码,PHPUnit通过检测我们编写的测试用例调用了哪些函数,哪些类,哪些方法,每一个控制流程是否都执行了一遍来计算覆盖率。 同时需要使用 这样我们就对业务代码 基境共享测试数据 可能你会发现我们在每个测试方法中都创建了 我们没有办法在 即测试的执行模式并不是 而是 所以 当运行测试时,每个测试类大致就是如下的执行步骤 所以我们可以在测试类构建时使用 或者使用 理解测试执行的模式还是很有帮助的,其他高级特性请浏览官方文档。 使用phpunit.xml编排测试套件 使用测试套件来管理测试, 然后直接运 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。Composer
安装 PHPUnit
#查看composer的全局bin目录 将其加入系统 path 路径 方便后续直接运行安装的命令composer global config bin-dir --absolute#全局安装 phpunitcomposer global require --dev phpunit/phpunit#查看版本phpunit --version
unit
项目用于演示单元测试的基本工作流mkdir unit && cd unit && mkdir app tests reports#结构如下./├── app #存放业务代码├── reports #存放覆盖率报告└── tests #存放单元测试
#一路回车即可composer init#注册命名空间vi composer.json... "autoload": { "psr-4": { "App\\": "app/", "Tests\\": "tests/" } }...#更新命名空间composer dump-autoload#安装 phpunit 组件库composer require --dev phpunit/phpunit
app/Example.php
这里我为节省排版就不写注释了msg = $value; } public function getMsg() { return $this->msg; }}
tests/ExampleTest.php
getTrue(); $this->assertTrue($result); } public function testGetFalse() { $example = new Example(); $result = $example->getFalse(); $this->assertFalse($result); } public function testGetMsg() { $example = new Example(); $result = $example->getTrue(); // $result is world not big_cat $this->assertEquals($result, "hello big_cat"); }}
[root@localhost unit]# phpunit --bootstrap=vendor/autoload.php \tests/PHPUnit 6.5.14 by Sebastian Bergmann and contributors...F 3 / 3 (100%)Time: 61 ms, Memory: 4.00MBThere was 1 failure:1) Tests\ExampleTest::testGetMsgFailed asserting that 'hello big_cat' matches expected true./opt/unit/tests/ExampleTest.php:27/root/.config/composer/vendor/phpunit/phpunit/src/TextUI/Command.php:195/root/.config/composer/vendor/phpunit/phpunit/src/TextUI/Command.php:148FAILURES!Tests: 3, Assertions: 3, Failures: 1.
PHPUnit
手册学习更多高级用法。测试用例
对测试对象
的行,函数/方法,类/特质
的访问率是多少(PHP_CodeCoverage
尚不支持 Opcode覆盖率、分支覆盖率 及 路径覆盖率
),虽然有很多人认为过分看重覆盖率是不对的,但我们初入测试还是俗气的追求一下吧。PHPUnit
的覆盖率依赖 Xdebug
,可以生成多种格式:--coverage-clover
--whitelist dir
参数来设定我们需要检测覆盖率的业务代码路径,下面演示一下具体操作:phpunit \--bootstrap vendor/autoload.php \--coverage-html=reports/ \--whitelist app/ \tests/#查看覆盖率报告cd reports/ && php -S 0.0.0.0:8899
App\Example
做单元测试,并且获得我们单元测试的代码覆盖率,现在自然是百分之百,因为我的测试用例已经访问了App\Example
的所有方法,没有遗漏的,开发中则能体现出你的测试时用力对业务代码测试度的完善性。App\Example
对象,在一些场景下是重复劳动,为什么不能只创建一次然后供其他测试方法访问呢?这需要理解 PHPUnit 执行测试用例的工作流程。不同的测试方法
中通过某成员属性
来传递数据,因为每个测试方法
的执行都是新建
一个测试类对象
,然后调用相应的测试方法
。testObj = new ExampleTest();testObj->testMethod1();testObj->testMethod2();
testObj1 = new ExampleTest();testObj1->testMethod1();testObj2 = new ExampleTest();testObj2->testMethod2();
testMethod1()
修改的属性状态
无法传递给 testMethod2()
使用。PHPUnit
则为我们提供了全面的hook
接口:public static function setUpBeforeClass()/tearDownAfterClass()//测试类构建/解构时调用protected function setUp()/tearDown()//测试方法执行前/后调用protected function assertPreConditions()/assertPostConditions()//断言前/后调用
#测试类基境构建setUpBeforeClass#new一个测试类对象#第一个测试用例setUpassertPreConditionsassertPostConditionstearDown#new一个测试类对象#第二个测试用例setUpassertPreConditionsassertPostConditionstearDown...#测试类基境解构tearDownAfterClass
setUpBeforeClass
创建一个 App\Example
对象作为测试类的静态成员变量(tearDownAfterClass
主要用于一些资源清理,比如关闭文件,数据库连接),然后让每一个测试方法用例使用它:setMsg("hello big_cat"); $result = self::$example->getTrue(); $this->assertTrue($result); } public function testGetFalse() { $result = self::$example->getFalse(); $this->assertFalse($result); } /** * 依赖 testGetTrue 执行完毕 * @depends testGetTrue * @return [type] [description] */ public function testGetMsg() { $result = self::$example->getMsg(); $this->assertEquals($result, "hello big_cat"); }}
@depends
注解来声明二者的执行顺序,并使用传递参数
的方式来满足需求。public function testMethod1(){ $this->assertTrue(true); return "hello";}/** * @depends testMethod1 */public function testMethod2($str){ $this->assertEquals("hello", $str);}
#执行模式大概如下testObj1 = new Test;$str = testObj1->testMethod1();testObj2 = new Test;testObj2->testMethod2($str);
vi phpunit.xml
:phpunit
行即可:[root@localhost unit]# phpunit PHPUnit 6.5.14 by Sebastian Bergmann and contributors.Time: 81 ms, Memory: 4.00MBNo tests executed!Generating code coverage report in HTML format ... done
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号