开发者

PHPUnit fails in Netbeans with Namespaces

I have: PHP 5.3.8, PHPUnit 3.5.15, Netbeans 7.0.1

While using the standard example of Netbeans for PHPUnit testing, it runs perfectly. By adding just the "namespace test;" I get the error that Calculator.php is not a file nor a directory. How to solve this problem? (I would like to use the namespace declarative in my project)

THE CLASS TO TEST:

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;
}

}

?>

THE UNIT TEST:

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 answer is, of course, correct. As a little addon:

From what i understand it has become common practice to put your tests in the same namespace as your production class.

namespace test;
require_once dirname(__FILE__) . '/../Calculator.php';

class CalculatorTest extends \PHPUnit_Framework_TestCase {

then you can continue using

$this->object = new Calculator:


Learn about using namespaced classes in your code. You need to create Calculator class instance not with:

$this->object = new Calculator;

but:

$this->object = new \test\Calculator;

I assume that class is loaded. If not see your autoloader or correct file path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜