phpunit中文文档:
目录结构:
test ├── controllers │ ├── BaseControllerTest.php │ └── IndexControllerTest.php ├── modules │ └── Testdemo
│ │ ├── controllers
│ │ │ ├── ApiTest.php
├── index.php ├── phpunit.xml └── composer.json
在tests目录下放置composer.json文件:
{
"require-dev": {
"phpunit/phpunit": "^6.1",
"phpunit/php-invoker": "^1.1",
"phpunit/dbunit": "^3.0"
}
}
安装composer:composer install
入口文件配置:tests/index.php
<?php
date_default_timezone_set("Asia/Shanghai");
define('ENVIRONMENT', 'develop');
include './vendor/autoload.php';
define('APPLICATION_PATH', dirname(dirname(__FILE__)));
define("VIEWS_PATH", APPLICATION_PATH."/application/views");
$application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini");
$application->bootstrap()->run();
?>
在tests/phpunit.xml文件中引入入口文件:
<phpunit bootstrap="./index.php">
<testsuites>
<!--<testsuite name="controllers">-->
<!--<file>./controllers/DemoTest.php</file>-->
<!--</testsuite>-->
<!--<testsuite name="models">-->
<!--<file>./models/UserTest.php</file>-->
<!--</testsuite>-->
<!--<testsuite name="models/Mysql">-->
<!--<file>./models/Mysql/UserTest.php</file>-->
<!--</testsuite>-->
<!--<testsuite name="models/Redis">-->
<!--<file>./models/Redis/UserTest.php</file>-->
<!--</testsuite>-->
<!--<testsuite name="modules/Testdemo">-->
<!--<file>./modules/Testdemo/controllers/ApiTest.php</file>-->
<!--</testsuite>-->
</testsuites>
</phpunit>
在tests目录下运行ApiTest测试文件命令行:
phpunit --stderr ./modules/Testdemo/controllers/ApiTest.php 注:--stderr 是指直接输出,不会经过缓存,屏蔽header带出来的错误
phpunit --stderr --filter 'testIndex' ./modules/Testdemo/controllers/ApiTest.php 注:'testIndex'是指只运行ApiTest.php里边的Index测试块,即testIndex方法