使用 PHPUnit 为 Package 编写单元测试

使用 PHPUnit 为 Package 编写单元测试 QQ群:595928952 上一节 下一节

开发 Laravel 扩展 >> 使用 PHPUnit 为 Package 编写单元测试 视频发布于 2016-09-20

在衡量一个 Package 的质量的时候,单元测试的通过率和覆盖率其实是一个很重要的指标,本节视频主要是简单上手 PHPUnit ,来为我们编写的 Laravel 扩展写单元测试,希望能唤起你 “测试很重要” 的念头。
daimingkang

看完了!感觉这个部分不错

chenze007

可算 触摸到 php的单元测试了
之前一直想了解这一块 一直没有机会跟精力看那些英文的文档
感谢你jelly

JellyBool

后面出个 完整的系列

TXZ

我的package里面用到了Laravel提供的辅助函数,测试说未定义的方法,怎么解决啊?
还有,我貌似问了很多问题你都没回过,jelly。。。

JellyBool

大概是修改 phpunit.xml 的 bootstrap 配置就可以了:

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>

参照 laravel 自带的 bootstrap 的配置,目录相对应改一下就可以

TXZ

不是太明白,我已经将bootstrap改成"vendor/bootstrap.php"了

JellyBool

这一行:

bootstrap="bootstrap/autoload.php"

使用 laravel 框架自带的 bootstrap/autoload.php 文件作为测试的 bootstrap 配置才可以使用 laravel 自带的方法吧

TXZ

那需要全局安装phpunit吗?

TXZ

换成这个错误变成了打不开bootstrap/autoload.php,要改文件权限吗?

JellyBool

额。。。路径确定写对了么?应该不是权限问题吧

TXZ

这里写相对路径无效啊?
其实还有几个问题,我写的package依赖有配置文件,当用户没有vendor:publish的时候,config会报错,怎么解决啊?
最大的问题还是测试,不会用phpunit,很简单的代码,肯定不会有问题,但是就是phpunit测试会失败,laravel对这方面的文档太少了。
jelly,只有靠你了。

JellyBool

额,我觉得你目前可能需要了解的是:

1.为什么要测试
2.怎么才算测试
3.有哪些途径可以测试

文档的话:https://laravel.com/docs/5.3/packages#routing 看 Default Package Configuration 这里

TXZ

我暂时用绝对路径的方式引用bootstrap/bootstrap.php,会报错:

/home/vagrant/Code/Packages/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:62
/home/vagrant/Code/Packages/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:166
/home/vagrant/Code/Packages/packages/cals/validator/src/AjaxValidator.php:17
/home/vagrant/Code/Packages/packages/cals/validator/tests/AjaxValidatorTest.php:26
/home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/Command.php:185
/home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/Command.php:115
PHP Fatal error:  Uncaught Error: Call to undefined method PHPUnit_Framework_TestResult::warnings() in /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php:297
Stack trace:
#0 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php(187): PHPUnit_TextUI_ResultPrinter->printWarnings(Object(PHPUnit_Framework_TestResult))
#1 /home/vagrant/Code/Packages/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(446): PHPUnit_TextUI_ResultPrinter->printResult(Object(PHPUnit_Framework_TestResult))
#2 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/Command.php(185): PHPUnit_TextUI_TestRunner->doRun(Array, true)
#3 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/Command.php(115): PHPUnit_TextUI_Command->run(Array, true)
#4 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
#5 {main}
  thrown in /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php on line 297

Fatal error: Uncaught Error: Call to undefined method PHPUnit_Framework_TestResult::warnings() in /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php:297
Stack trace:
#0 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php(187): PHPUnit_TextUI_ResultPrinter->printWarnings(Object(PHPUnit_Framework_TestResult))
#1 /home/vagrant/Code/Packages/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(446): PHPUnit_TextUI_ResultPrinter->printResult(Object(PHPUnit_Framework_TestResult))
#2 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/Command.php(185): PHPUnit_TextUI_TestRunner->doRun(Array, true)
#3 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/Command.php(115): PHPUnit_TextUI_Command->run(Array, true)
#4 /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
#5 {main}
  thrown in /home/vagrant/Code/Packages/packages/cals/validator/vendor/phpunit/phpunit/src/TextUI/ResultPrinter.php on line 297