开发者

PHPUnit error - Class could not be found

开发者 https://www.devze.com 2023-03-05 14:16 出处:网络
I just installed PHPUnit 3.5 on my system, upgrading it from 3.4, and I\'m having some trouble with the new version. When I try to run a test, I always get the same output. Here\'s what I get when I t

I just installed PHPUnit 3.5 on my system, upgrading it from 3.4, and I'm having some trouble with the new version. When I try to run a test, I always get the same output. Here's what I get when I try to run on the command line the StackTest example from the PHPUnit manual, example 4.1:

> phpunit StackTest

X-Powered-By: PHP/5.2.17
Content-type: text/html

PHPUnit 3.5.13 by Sebastian Bergmann.

Class StackTest could not be found in StackTest.php.

Worse yet, when I try to run it from a web browser, I get the following output:

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /path/to/tests/StackTest.php on line 2 
开发者_如何学Python

Does anyone know how to set this up? Thanks.


I had the problem you described on Windows. The problem was in the file pear\PHPUnit\Runner\StandardTestSuiteLoader.php on line 131 an it was caused by different drive letter case in file name in the condition

if ($class->getFileName() == realpath($suiteClassFile)) {

My simple fix is to change this line to be case insensitive

if (strtolower($class->getFileName()) == strtolower(realpath($suiteClassFile))) {


phpunit MyTestClass

In my case

  • MyTestClass.php should be in the project home directory
  • it should starts with long php open tag (<?php, not <?)
  • it should contain class MyTestClass extends PHPUnit_Framework_TestCase {

I know this is most likely not the best way, just point for a beginner to start with.


Try

pear upgrade pear

(if it asks you to channel upgrade do so)

and then

pear install --force --alldeps phpunit/phpunit

and try again.

The 3.5 upgrade combined with a buggy pear installer (1.9.1 has a kinda annoying bug so make sure you are really on 1.9.2) can be a pain sometimes.


I think your PHPUnit Class named StackTest, and the class you want to test is also named StackTest. This will cause a path conflict in PHPUnit.

Make these 2 names different and you will get this resolved.


In my case, this problem was caused by including PHPUnit in the source file via require_once:

require_once 'phar://phpunit.phar';

Removing that line made my test case runnable.


This error can also be caused when you forget to have your test class extend the PHPUnit TestCase class, like

class MyTestClass extends \PHPUnit\Framework\TestCase { ...


I was able to fix the problem. It was a result of how I was loading the class. I used my arguments in the argument array like so and it worked. But there were a lot of other problems with the classpath etc that I had to fix first. To see a working solution look here (http://www.siteconsortium.com/h/p1.php?id=php002).

$command = new PHPUnit_TextUI_Command();
$command->run(array('test', 'testCase', 'c:\workspace\project\testCase.php'), true);


Starting from PHPUnit 9, it is required that the filename match the class name in the test.

#4105: Deprecate multiple test case classes in single file and test case class name differing from filename

So test-plugin.php with a class name PluginTest will fail with this error. To fix it, you'd need to rename the file to PluginTest.php.

Bad error message IMO.


It sounds like PHPUnit isn't on your include path. To easily test this, try this:

$ phpunit --include-path /path/to/PHPUnit StackTest 
0

精彩评论

暂无评论...
验证码 换一张
取 消