问题
I have a PHP project, with the following project structure.
php_test_app
src
Vegetable.php
tests
StackTest.php
VegetableTest.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.
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.
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.
- How should I make the include such that the unit tests are recognized?
- Do I need to specify the full path, or just the name of the file in which the class is defined?
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);
}
}
?>
回答1:
phpunit --bootstrap src/Vegetable.php tests
instructs PHPUnit to load src/Vegetable.php
before running the tests found in tests
.
Note that --bootstrap
should be used with an autoloader script, for instance one generated by Composer or PHPAB.
Also have a look at the Getting Started section on PHPUnit's website.
来源:https://stackoverflow.com/questions/29232027/phpunit-cannot-find-source-code