标签:测试文件 term load ade tps director class 配置 ESS
wget https://phar.phpunit.de/phpunit-7.0.phar
chmod +x phpunit-7.0.phar
mv phpunit-7.0.phar /usr/local/bin/phpunit
phpunit --version
mkdir src
mkdir tests
<?php
/*
@desc:自动加载带命名空间的类的函数
@param dir 需要注册自动加载的文件夹
*/
function autoloader($dir){
spl_autoload_register(function($name) use ($dir){
$name = str_replace(‘\\‘,DIRECTORY_SEPARATOR,$name);
require $dir.DIRECTORY_SEPARATOR.$name.‘.php‘;
});
}
define(‘ROOT‘,__DIR__);
autoloader(ROOT);
b. 测试类:
vim src/Money.php
<?php
class Money
{
private $amount;
public function __construct($amount)
{
$this->amount = $amount;
}
public function getAmount()
{
return $this->amount;
}
public function negate()
{
return new Money(-1*$this->amount);
}
}
c. 测试文件:
vim tests/MoneyTest.php
<?php
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase
{
public function testCanBeNegated()
{
$a = new Money(1);
$b = $a->negate();
$this->assertEquals(-1, $b->getAmount());
}
}
phpunit --bootstrap src/autoload.php tests/MoneyTest.php
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="money">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
b. 执行命令:
phpunit tests/MoneyTest
标签:测试文件 term load ade tps director class 配置 ESS
原文地址:http://blog.51cto.com/12173069/2317104