-->

How do I solve this error: “Class PHPUnit_Extensio

2019-03-24 21:12发布

问题:

I am trying to run a SeleniumTestCase with phpunit but I cannot get it to run with the phpunit.bat script.

My goal is to use phpunit with Selenium RC in CruiseControl & phpUnderControl. This is what the test looks like:

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class WebTest extends PHPUnit_Extensions_SeleniumTestCase
{
    protected function setUp()
    {
        $this->setBrowser('*firefox');
        $this->setBrowserUrl('http://www.example.com/');
    }

    public function testTitle()
    {
        $this->open('http://www.example.com/');
        $this->assertTitleEquals('Example Web Page');
    }
}

I also got PEAR in the include_path and PHPUnit installed with the Selenium extension. I installed these with the pear installer so I guess that's not the problem.

Any help would be very much appreciated.

Thanks, Remy

回答1:

hopefully this is a more definitive answer then the ones given here (which did not solve me problem). If you are getting this error, check your PEAR folder and see if the "SeleniumTestCase.php" file is actually there:

/PEAR/PHPUnit/Extensions/SeleniumTestCase.php

If it is NOT, the easiest thing to do is to uninstall and reinstall PHPUnit using PEAR ...

pear uninstall phpunit/PHPUnit

pear uninstall phpunit/PHPUnit_Selenium

pear install phpunit/PHPUnit

After doing the above and doing just the single install, PHPUnit_Selenium was also auto installed, I'm not sure if this is typical, so some might have to do...

pear install phpunit/PHPUnit_Selenium

Also see http://www.phpunit.de/manual/3.5/en/installation.html for PEAR channel info if needed...



回答2:

Here is the deal:

If you have a "Class PHPUnit_Extensions_SeleniumTestCase could not be found in (testcase file name)" problem, you have to do the following two things:

1. Rename the file of test case to the name of the class it contains 2. You should launch phpunit from the folder with your tests.

This should fix your problem.

Andrew



回答3:

Have a look at one of the comments in the require_once entry in the php manual..

http://ie.php.net/manual/en/function.require-once.php#62838

"require_once (and include_once for that matters) is slow

Furthermore, if you plan on using unit tests and mock objects (i.e. including mock classes before the real ones are included in the class you want to test), it will not work as require() loads a file and not a class."



回答4:

I just renamed the file my test was in to "WebTest.php" (the name of the class it contains) and the test runs fine now.



回答5:

Do not presume that the pear install occurred without problems.

I had installed phpunit through pear but despite it saying the install went fine when I looked inside the folder, I had all these files starting with .tmp, eg PHPUnit/Util/.tmpErrorHandler.php so naturally when i ran a test for the 1st time it gave me the same error as above. After checking that indeed the file wasn't there I did a manual install of PHPUnit to the same folder as pear and alas, all was fine. I'm in Mac/leopard.

About Selenim RC don't forget to start it by running in terminal java -jar /path/to/file/selenium-server.jar



回答6:

I found that the following sample from PHPUnit tutorial was working while the same error appeared in the test that I had written. The solution was a surprise. Ensure that your class is inside a <?php .. ?> block and not a <? .. ?> block in the script.

<?php
require_once 'PHPUnit/Framework.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));
    }
}
?>


回答7:

Here is how i solved this problem:

  1. Make sure that curl extension for php is installed, e.g for ubuntu sudo apt-get install php5-curl
  2. Enter sudo pear install phpunit/PHPUnit_Selenium

After that you should have the missing file installed

Happy coding...



回答8:

Try:

class WebTest extends \PHPUnit_Extensions_Selenium2TestCase

It can be namespace issue, as it was for me.



回答9:

Well when I use inline command : if lauching test from PhPunit dir i have the error while whent launching it from test dir I havne't the error ...

but I still haven't any acces to selenium server ... shall I have to launch it before or not.

If Yes it's strange that we havne't to specify any handle to PhPUnit ...



回答10:

When it fails it doesn't always print out the most verbose error messages.

Always remember to start Selenium too prior to running test.

java -jar selenium-server-standalone-2.39.0.jar

Here is an example of code that was working for myself. http://www.siteconsortium.com/h/p1.php?id=php002. Obviously there are a lot of different way to write the test suite, and launch the test case but I used the set_class_path to get rid of class issues at first.