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

发布于 2024-12-03 20:58:18 字数 1907 浏览 0 评论 0原文

我有:PHP 5.3.8、PHPUnit 3.5.15、Netbeans 7.0.1

在使用 Netbeans 的标准示例进行 PHPUnit 测试时,它运行得很好。 通过仅添加“命名空间测试;”我收到错误消息 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)
    );
}

}

?>

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

}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-12-10 20:58:18

@Tomasz 的答案当然是正确的。作为一个小插件:

据我了解,将测试放在与生产类相同的命名空间中已成为常见做法。

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

class CalculatorTest extends \PHPUnit_Framework_TestCase {

然后你可以继续使用

$this->object = new Calculator:

@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:
天赋异禀 2024-12-10 20:58:18

了解如何在代码中使用命名空间类。您需要创建计算器类实例,而不是:

$this->object = new Calculator;

但是:

$this->object = new \test\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文