PHPUnit 在带有命名空间的 Netbeans 中失败

标签 php netbeans namespaces phpunit

我有:PHP 5.3.8、PHPUnit 3.5.15、Netbeans 7.0.1

在使用Netbeans标准示例进行PHPUnit测试时,运行完美。 通过仅添加“ namespace 测试”;我得到的错误是 Calculator.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:

关于PHPUnit 在带有命名空间的 Netbeans 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7377599/

相关文章:

c++ - 如何将英特尔 C++ 编译器与 NetBeans (Windows) 结合使用

c++ - 是否可以在 C++ 的命名空间中放置一个宏?

linux - 一种转换音频文件比特率/格式的方法(在上传和存储到 S3 之间)

java - 编译器如何区分从不同实例化创建的相同变量

java - 如何混淆基于java的项目

java - 如何将 TreeModelListener 添加到 NetBeans 大纲?

c++ - 包容性问题

c# - 找不到元数据文件 .dll

javascript - 在 JavaScript 中插入 MySQLi

java - 集成 PHP 和 Java?