Phpunit: Testcase does not open, throws ' Cannot open file "mytest.php" '

Created on 24 Jun 2011  ·  19Comments  ·  Source: sebastianbergmann/phpunit

Hi,
I have installed PHPUnit 3.5.14 using the Pear package installation on my sandbox that runs CentOS 5.2

When I tried to execute my testcases located at the location:

    /var/www/testcases/mytest.php

using the terminal command:

    phpunit mytest.php

This is throwing me the following error:

PHPUnit 3.5.14 by Sebastian Bergmann.

Cannot open file "mytest.php".

I have even tried to uninstall and reinstall phpunit, but then its still throwing the same error.
The code inside mytest.php is as follows:

class mytest extends PHPUnit_Framework_TestCase
{
public function testOne()
{
$this->assertTrue(FALSE);
}

/**
 * @depends testOne
 */
public function testTwo()
{
}

}
?>

Any help would be highy appreciated.
Thanks for looking into my post.

Regards,
LoGiX

Most helpful comment

I am still having this issue. I call php unit on the file with and without the ".php" extension. I reference the bootstrap.php file in my xml correctly and I also have tried referencing it explicitly with "--bootstrap". I can also run another test file in the same folder just fine. All of them actually except this one. They all have the same permissions. OrganizationTest.php is the problem file.

noahgary@Noahs-MBP:~/Projects/han-api/infrastructure/Testing/Unit$ ls -l
total 80
-rw-r--r--  1 noahgary  staff   2.2K Apr 12 12:59 CartTypeTest.php
-rw-r--r--  1 noahgary  staff   2.4K Apr 12 12:59 ConditionTest.php
-rw-r--r--  1 noahgary  staff   2.4K Apr 12 12:59 CountryTest.php
-rw-r--r--  1 noahgary  staff   2.2K Apr 12 12:59 FeeTypeTest.php
-rw-r--r--  1 noahgary  staff   2.6K Apr 12 12:59 OrderItemStatusTest.php
-rw-r--r--  1 noahgary  staff   2.4K Apr 12 12:59 OrderStatusTest.php
-rw-r--r--  1 noahgary  staff   2.6K Apr 12 17:05 OrganizationTest.php
-rw-r--r--  1 noahgary  staff   2.7K Apr 12 12:59 OrganizationTypeTest.php
-rw-r--r--  1 noahgary  staff   2.5K Apr 12 12:59 QuantityTypeTest.php
-rw-r--r--  1 noahgary  staff   2.3K Apr 12 16:52 UserTest.php

UPDATE: The test runs if I run it with all the others. I cannot run it alone though which adds approx. 5 min to each run while I'm trying to get these tests working.

UPDATE-2: Quick workaround is the "--filter" flag. It allows you to run just one test out of the batch.

e.g.
vendor/bin/phpunit --filter OrganizationTest

All 19 comments

Are you in the directory containing the test case? Do you have a bootstrap.php file that sets up the include path appropriately?

Are you using "tab" for complete the address to the file? if you are doing that stop doing it, write the address manually :)

Seems more like a file permission or some other kind of error than a phpunit problem. As I've got no way of reproducing this I'll close it. Feel free to reopen if needed

Disclaimer: Posting on this closed issue because is the closest I got of an issue like mine, and it might be useful to someone else...

For anyone that may still be facing this issue, check if 'safe_mode = On' on /etc/php5/cli/php.ini.
Switching it to 'safe_mode = Off' will do the trick.
In my case, I was trying to execute files owned by the current user - which is not root - that in turn would include PHPUnit files - owned by root. Something not allowed when safemode is on. Both users must have the same id.

It would have saved me hours of hair pulling if I found out it earlier :/

Thx alfetopito!

In case anybody fall here from a Google search, I had the same problem, but my file was Bootstrap.php and I solved the issue renaming its name to bootstrap.php (all lowercase).

i had the same problem, the its was a path issue, kindly check that you are pointing your bash terminal to the correct path.

Ran through same issue, had not fixed path to the bootstrap.php on the phpunit.xml file...

I had include_path sections separated by comma. It has to be semicolon on Windows.

I have been trying to install PHPunit for 5days!. now its working and am having this error which might be the last I will have.

I am geting "cannot open file filename.php"

please help!!

@pumamammal I would like to understand why it took you five days to install PHPUnit as I would like to improve the documentation accordingly. Can you please open a ticket at https://github.com/sebastianbergmann/phpunit-documentation/issues/new and explain where https://phpunit.de/getting-started.html and https://phpunit.de/manual/current/en/installation.html failed you?

As for your cannot open file filename.php issue, @pumamammal, please understand that I can only make sense of complete output and not just random snippets, sorry.

I'm having the same error too after working through a few other nasty ones. I've been at this for 5 days nonstop as well. Safe mode is off, permissions are 777. I don't have a bootstrap.php file. Is that necessary? If so, is there any documentation on how to create one? I'm a novice developer.

I'm getting same error too, but when I don't specify a file it run with no problem.
I'm a newbie too.
PHPUnit 5.4.6, installed via Composer 1.1.2, Windows 10 64 bit.

I'm getting the same error too, and I try to solve it through the following steps

  • require the autoload.php file
  • I try to using the command "phpunit myFile.php", adding ".php"
    and it work. though I know nothing.
    phpunit 5.5.0, windows 10 64 bit, php5.6

I solved the problem!!
Correct syntax: phpunit tests/PagesTest .
Inspired by https://phpunit.de/manual/current/en/organizing-tests.html#organizing-tests.filesystem

Thank you

In my case, just adding '.php' at the end of the filename worked.
Instead of
'phpunit CarTest'
use
'phpunit CarTest.php'.

I hope it helps. :D

if you use the full path it should be okay for example when I used unit --bootstrap "C:\xampp\htdocs\general-php\vendor\autoload.php" tests issue resolved

I am still having this issue. I call php unit on the file with and without the ".php" extension. I reference the bootstrap.php file in my xml correctly and I also have tried referencing it explicitly with "--bootstrap". I can also run another test file in the same folder just fine. All of them actually except this one. They all have the same permissions. OrganizationTest.php is the problem file.

noahgary@Noahs-MBP:~/Projects/han-api/infrastructure/Testing/Unit$ ls -l
total 80
-rw-r--r--  1 noahgary  staff   2.2K Apr 12 12:59 CartTypeTest.php
-rw-r--r--  1 noahgary  staff   2.4K Apr 12 12:59 ConditionTest.php
-rw-r--r--  1 noahgary  staff   2.4K Apr 12 12:59 CountryTest.php
-rw-r--r--  1 noahgary  staff   2.2K Apr 12 12:59 FeeTypeTest.php
-rw-r--r--  1 noahgary  staff   2.6K Apr 12 12:59 OrderItemStatusTest.php
-rw-r--r--  1 noahgary  staff   2.4K Apr 12 12:59 OrderStatusTest.php
-rw-r--r--  1 noahgary  staff   2.6K Apr 12 17:05 OrganizationTest.php
-rw-r--r--  1 noahgary  staff   2.7K Apr 12 12:59 OrganizationTypeTest.php
-rw-r--r--  1 noahgary  staff   2.5K Apr 12 12:59 QuantityTypeTest.php
-rw-r--r--  1 noahgary  staff   2.3K Apr 12 16:52 UserTest.php

UPDATE: The test runs if I run it with all the others. I cannot run it alone though which adds approx. 5 min to each run while I'm trying to get these tests working.

UPDATE-2: Quick workaround is the "--filter" flag. It allows you to run just one test out of the batch.

e.g.
vendor/bin/phpunit --filter OrganizationTest

Same for me. Simply adding the "--filter" flag worked. Otherwise it didn't.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AnmSaiful picture AnmSaiful  ·  4Comments

TiMESPLiNTER picture TiMESPLiNTER  ·  3Comments

sebastianbergmann picture sebastianbergmann  ·  4Comments

greg0ire picture greg0ire  ·  4Comments

dciancu picture dciancu  ·  3Comments