7

I have this in my phpunit.xml file:

<phpunit ...>
    <testsuites>
        <testsuite name="MyTests">
            <directory>../path/to/some/tests</directory>
        </testsuite>
    </testsuites>
    ... // more settings for <filter> and <logging>
</phpunit>

And when I go to run it, I get this error:

PHP fatal error: Uncaught exception 'PHPUnit_Framework_Exception'
with message 'Neither "MyTests.php" nor "MyTests.php" could be opened.'

Why does PHPUnit give me this error, and why is it looking for "MyTests.php" if I gave it a directory in which to look for tests?

And on a related note, when I add more <testsuite> entries with other tests, PHPUnit runs without error. What's up with that?

2 Answers 2

5

By default PHPUnit will add "all *Test classes that are found in *Test.php files" (see PHPUnit docs). If it doesn't find any files matching that description, (e.g. a SomeTest.php file defining a SomeTest class), it falls back to looking for a file based on the name attribute of the test suite.

The solution is to create a file matching that description so that PHPUnit doesn't fall back to its default of searching by the testsuite name:

<?php
// in ../path/to/some/tests/SomeTest.php:
class SomeTest extends PHPUnit_Framework_TestCase {
    public function test() {
        //... test cases here
    }
}
?>

Now you should be able to run phpunit without errors:

$ phpunit
PHPUnit 3.5.14 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 10.75Mb

OK (1 test, 0 assertions)

It will work without errors when you add more testsuite entries if PHPUnit is able to find matching test cases to run under those other suites. If it finds tests to run in any testsuite, it won't resort to searching by the name attribute for the suites it couldn't find anything for.

Sign up to request clarification or add additional context in comments.

Comments

1

I believe the problem is that you're not telling it which files contain the test cases and/or suites you want to run. Try adding the suffix="Test.php" attribute.

<testsuite name="MyTests">
    <directory suffix="Test.php">../path/to/some/tests</directory>
</testsuite>

2 Comments

It looks like default suffix is "Test.php"; it's when there are no files matching the suffix that it starts looking for a file with the name given in the name attribute. Interesting.
LOL, I assumed you actually had some tests to run and looked for what could be different compared to my file. :) Good to know I can omit the suffix from now on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.