PHPUnit找不到源代码 [英] PHPUnit cannot find source code
问题描述
我有一个PHP项目,具有以下项目结构.
I have a PHP project, with the following project structure.
php_test_app
src
Vegetable.php
tests
StackTest.php
VegetableTest.php
这些文件的代码如下所示.我在Eclipse中使用PDT和PTI.Eclipse中的PHPUnit认识到 VegetableTest.php
属于 Vegetable.php
,因为您可以使用切换按钮在它们之间进行切换.
The code of these files is shown below. I use PDT and PTI in Eclipse. PHPUnit in Eclipse recognizes that VegetableTest.php
belongs to Vegetable.php
, because you can toggle between them using the toggle button.
我首先尝试通过在PHP资源管理器中选择tests目录来运行测试代码,然后单击 Run Selected PHPUnit Test
.它运行两个测试,两个 VegetableTest
均失败,并显示以下跟踪信息:致命错误:在/Users/erwin/Documents/workspace/php_test_app/tests/VegetableTest.php中找不到类'Vegetable'在第8行上
.一个类似的问题发布在这里: phpunit找不到类,PHP致命错误.
I first try to run the test code by selecting the tests directory in the PHP Explorer and click Run Selected PHPUnit Test
. It runs both tests, both the VegetableTest
fails with the following trace: Fatal error: Class 'Vegetable' not found in /Users/erwin/Documents/workspace/php_test_app/tests/VegetableTest.php on line 8
. A similar issue was posted here: phpunit cannot find Class, PHP Fatal error.
实际上,我还没有包含我的源代码,所以现在我取消注释 VegetableTest.php
中的包含,如下所示.如果现在尝试以相同的方式运行测试,PHPUnit将无法识别任何测试代码!甚至未更改的 StackTest
也不会被识别.
Indeed, I haven't included my source code yet, so now I uncomment the include in VegetableTest.php
, shown below. If I now try to run the tests in the same way, PHPUnit does not recognize any test code! Even the StackTest
, which is unaltered, is not recognized.
- 我应该如何进行包含,以使单元测试成为认可?
- 我需要指定完整路径还是仅指定名称?定义类的文件?
更改include语句也行不通;我尝试了以下方法.
Changing the include statement also doesn't work; I have tried the following.
include 'Vegetable.php';
include 'src/Vegetable.php';
include '../src/Vegetable.php';
Vegetable.php
<?php
// base class with member properties and methods
class Vegetable {
var $edible;
var $color;
function Vegetable($edible, $color="green")
{
$this->edible = $edible;
$this->color = $color;
}
function is_edible()
{
return $this->edible;
}
function what_color()
{
return $this->color;
}
} // end of class Vegetable
// extends the base class
class Spinach extends Vegetable {
var $cooked = false;
function Spinach()
{
$this->Vegetable(true, "green");
}
function cook_it()
{
$this->cooked = true;
}
function is_cooked()
{
return $this->cooked;
}
} // end of class Spinach
StackTest.php
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
?>
VegetableTest.php
<?php
// require_once ('../src/Vegetable.php');
class VegetableTest extends PHPUnit_Framework_TestCase
{
public function test_constructor_two_arguments()
{
$tomato = new Vegetable($edible=True, $color="red");
$r = $tomato.is_edible();
$this->assertTrue($r);
$r = $tomato.what_color();
$e = "red";
$this->assertEqual($r, $e);
}
}
class SpinachTest extends PHPUnit_Framework_TestCase
{
public function test_constructor_two_arguments()
{
$spinach = new Spinach($edible=True);
$r = $spinach.is_edible();
$this->assertTrue($r);
$r = $spinach.what_color();
$e = "green";
$this->assertEqual($r, $e);
}
}
?>
推荐答案
phpunit --bootstrap src/Vegetable.php测试
指示PHPUnit加载 src/Vegetable.php
在运行 tests
中找到的测试之前.
phpunit --bootstrap src/Vegetable.php tests
instructs PHPUnit to load src/Vegetable.php
before running the tests found in tests
.
请注意,-bootstrap
应该与自动加载器脚本一起使用,例如由 Composer 或 PHPAB .
Note that --bootstrap
should be used with an autoloader script, for instance one generated by Composer or PHPAB.
还可以查看PHPUnit网站上的入门部分.
Also have a look at the Getting Started section on PHPUnit's website.
这篇关于PHPUnit找不到源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!