1
2
3
4
5
|
$ wget https:
//phar.phpunit.de/phpunit-7.0.phar
$
chmod
+x phpunit-7.0.phar
$ sudo mv phpunit-7.0.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann
and
contributors.
|
1
|
composer
global
require
phpunit/phpunit
|
1
2
3
4
5
6
7
|
<
phpunit
bootstrap="vendor/autoload.php">
<
testsuites
>
<
testsuite
name="service">
<
directory
>tests</
directory
>
</
testsuite
>
</
testsuites
>
</
phpunit
>
|
先寫一個須要測試的類,該類有一個eat方法,方法返回字符串:eating,文件名爲Human.phpphp
<?php
class Human
{
public function eat()
{
return 'eating';
}
}
再寫一個phpunit的測試類,測試Human類的eat方法,必須引入Human.php文件、phpunit,文件名爲test1.php
<?php
include 'Human.php';
use PHPUnit\Framework\TestCase;
class TestHuman extends TestCase
{
public function testEat()
{
$human = new Human;
$this->assertEquals('eating', $human->eat());
}
}
?>
其中assertEquals方法爲斷言,判斷eat方法返回是否等於'eating',若是返回一直則成功不然返回錯誤,運行測試:打開命令行,進入test1.php的路徑,而後運行測試:
phpunit test1.php
返回信息:html
返回信息:
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.bootstrap
.composer
Time: 202 ms, Memory: 14.75MB測試
OK (1 test, 1 assertion)
則表示斷言處成功,即返回值與傳入的參數值一致。
ui