我有:PHP 5.3.8, PHPUnit 3.5.15, Netbeans 7.0.1
在使用Netbeans的标准示例进行PHPUnit测试时,它运行得很完美。只需添加"命名空间test;"我得到计算器。php不是文件也不是目录的错误。如何解决这个问题?(我想在我的项目中使用命名空间声明式)
要测试的类:namespace test;
class Calculator {
/**
* @assert (0, 0) == 0
* @assert (0, 1) == 1
* @assert (1, 0) == 1
* @assert (1, 1) == 2
* @assert (1, 2) == 4
*/
public function add($a, $b) {
return $a + $b;
}
}
?>
单元测试:
require_once dirname(__FILE__) . '/../Calculator.php';
/**
* Test class for Calculator.
* Generated by PHPUnit on 2011-09-11 at 00:52:24.
*/
class CalculatorTest extends PHPUnit_Framework_TestCase {
/**
* @var Calculator
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->object = new Calculator;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
/**
* Generated from @assert (0, 0) == 0.
*/
public function testAdd() {
$this->assertEquals(
0, $this->object->add(0, 0)
);
}
/**
* Generated from @assert (0, 1) == 1.
*/
public function testAdd2() {
$this->assertEquals(
1, $this->object->add(0, 1)
);
}
/**
* Generated from @assert (1, 0) == 1.
*/
public function testAdd3() {
$this->assertEquals(
1, $this->object->add(1, 0)
);
}
/**
* Generated from @assert (1, 1) == 2.
*/
public function testAdd4() {
$this->assertEquals(
2, $this->object->add(1, 1)
);
}
/**
* Generated from @assert (1, 2) == 4.
*/
public function testAdd5() {
$this->assertEquals(
4, $this->object->add(1, 2)
);
}
}
?>
@Tomasz的回答当然是正确的。作为一个小插件:
据我所知,将测试放在与生产类相同的命名空间中已经成为一种常见的做法。
namespace test;
require_once dirname(__FILE__) . '/../Calculator.php';
class CalculatorTest extends PHPUnit_Framework_TestCase {
则可以继续使用
$this->object = new Calculator:
了解如何在代码中使用命名空间类。您需要创建Calculator类实例,而不是使用:
$this->object = new Calculator;
但:
$this->object = new testCalculator;
我假设类已经加载。如果没有看到您的自动加载器或正确的文件路径。