composer安装phpunit并使用

1.安装phpunit

项目根目录创建composer.json ,编辑如下:

{
    "require-dev": {
        "phpunit/phpunit": "4.5.*"
    }
}

命令行执行composer install

2.单个文件测试

创建目录tests,新建文件UserTest.php,编辑如下:

#require __dir__.'/../vendor/autoload.php';
require_once(__dir__.'/../models/Users.php');
class UserTest extends PHPUnit_Framework_TestCase
{
  public function setUp(){ }
  public function tearDown(){ }
  public function testFindByOpenId()
  {
    $userModel = new Users() ;

  }
}

命令行执行:vendor\bin\phpunit tests\UserTest.php

3.测试套件

phpunit.xml:

<phpunit bootstrap="vendor/autoload.php">
  <testsuites>
    <testsuite name="money">
      <directory>testsdirectory>
    testsuite>
  testsuites>
phpunit>

命令行执行:vendor\bin\phpunit –configuration phpunit.xml

参考:
composer基本使用
PHP单元测试利器:PHPUNIT初探
PHPUnit手册
How to run a specific phpunit xml testsuite?

你可能感兴趣的:(PHP)