views:

1734

answers:

11

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  A: 

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."

Ciaran
+1  A: 

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.

Remy
A: 

Dear all, I was following the author's phpunit pocket guide and while trying the WebTest example I run in the same problem.

> phpunit "ex19-1 webTestSelenium.php"
> PHPUnit 3.3.1 by Sebastian Bergmann. 
> Class PHPUnit_Extensions_SeleniumTestCase could not be found in ex19-1 webTestSelenium.php;

I fix it, the way it is suggested here, but I don't know what really happen.

How the test's filename matters to the php class loader? Any explanation?

Thanks bitsurs

A: 

That's unbelievable, I've tried to fix the same problem by changing the filename - but this doesn't help! Any other ideas why this happens?

A: 

I had the same trouble and thank's to remy I change the name of the file and everything is now ok !!!

in fact i have no error ... but i can't connect to selenium RC server (which is running ...)

of course i'm under Eclipse for everything ...

Stephane

A: 

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 ...

A: 

I all again it occurs only with firefox ...

certainly a firefox conf trouble sorry

A: 

I also have the problem "Class PHPUnit_Extensions_SeleniumTestCase could not be found in (testcase file name)". Hix. I also tried rename file but it does not work. Please help me.

+5  A: 

Hey Guys, 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

A: 

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

sofia
A: 

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));
    }
}
?>
CruiZen
Also, try the flag --syntax-check for PHPUnit on the command line.Otherwise, PHPUnit may silently ignore the errors in your program.
CruiZen