如何在命令行开始使用 PHPUnit? [英] How to get started with PHPUnit at the command line?
问题描述
我有一台 MacBook Pro,安装了 PEAR,安装了 PHPUnit,所以我可以在命令行输入 phpunit代码>,我得到了使用帮助.
I have a MacBook Pro, installed PEAR, installed PHPUnit, so at the command line I can type phpunit
and I get the usage help.
现在我想进行测试,以便我可以从那里开始构建.
Now I want to get a test going so that I can build from there.
我有一个名为 index.php
的文件,其中包含以下内容:
I have a file named index.php
with this content:
<?php
require_once '?????';
class Product {
protected $id;
public function __construct($id)
{
$this->id = $id;
}
public function get_id()
{
return $this->id;
}
}
class ProductTest extends PHPUnit_Framework_TestCase
{
function testBasis()
{
$instance = new Product(1);
$this->assertInstanceOf('Product',$instance);
$this->assert($instance->get_id(), 1);
}
}
在命令行中,我想转到文件所在的目录并键入如下内容:
At the command line, I want to go to the directory in which the file is located and type something like:
phpunit ?????
接下来的步骤是什么,以便我能够从命令行使用 PHPUnit 测试上述类?
推荐答案
- 如果您正确安装了 phpunit,则不需要包含行.
class ProductTest 扩展了 PHPUnit_Framework_TestCase
- 保存文件
ProductTest.php
- 在命令行中使用cd"浏览到您保存 ProductTest.php 的目录
- 如果您正确安装了 phpunit,您应该可以输入
phpunit --verbose ProductTest.php
您的 ProductTest.php 文件必须如下所示:
Your ProductTest.php file will have to look like this :
<?php
class Product {
protected $id;
public function __construct($id)
{
$this->id = $id;
}
public function get_id()
{
return $this->id;
}
}
class ProductTest extends PHPUnit_Framework_TestCase
{
function testBasis()
{
$instance = new Product(1);
$this->isInstanceOf('Product',$instance);
$this->assertEquals($instance->get_id(), 1);
}
}
?>
在命令行运行 phpunit --verbose ProductTest
,将输出:
At the command line running phpunit --verbose ProductTest
, will output :
PHPUnit 3.4.13 by Sebastian Bergmann.
ProductTest
.
Time: 0 seconds, Memory: 6.50Mb
OK (1 test, 1 assertion)
dorin@ubuntu:/var/www$ phpunit --verbose ProductTest
PHPUnit 3.4.13 by Sebastian Bergmann.
ProductTest
.
Time: 0 seconds, Memory: 6.50Mb
OK (1 test, 1 assertion)
这篇关于如何在命令行开始使用 PHPUnit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!