gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:45:29 25 4
gpt4 key购买 nike

我有: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com