Alexander Morland
Alexander Morland

Reputation: 6454

phpunit does not find any tests with director xml tag, but does with (some) file tags

I have this config:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    backupGlobals               = "false"
    backupStaticAttributes      = "false"
    colors                      = "false"
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true"
    processIsolation            = "false"
    stopOnFailure               = "false"
    stopOnError                 = "false"
    stopOnIncomplete            = "false"
    syntaxCheck                 = "false"
    bootstrap                   = "test_bootstrap.php"
    >

  <testsuites>
      <testsuite name="UnitTests">
          <file>unit/api/2/ApiControllerTest.php</file>
          <file>unit/api/2/RoutesTest.php</file>
   </testsuite>

it runs the Test files. If I replace the files with

<directory>unit</directory>
// or
<directory>unit/api/2</directory>
// or
<directory>unit/api/2/*</directory>
// or
<directory>unit/api/2/*Test.php</directory>
// or
<directory suffix="Test.php">unit/api/2</directory>

It simply says: No tests executed!

Please, what could be wrong?

Ubuntu 12.04 LTS, php 5.3.10, phpunit 3.7.16

Upvotes: 3

Views: 2752

Answers (3)

Aubrey Lavigne
Aubrey Lavigne

Reputation: 652

PHPUnit defaults to the suffix Test.php, which isn't obvious.

If your tests use another suffix (e.g. TestUser.php or /test/User.php, instead of UserTest.php), the test will be skipped.

This default can be overwritten using the --test-suffix flag on the CLI:

phpunit --test-suffix _test.php

or with the suffix attribute in the XML configuration:

<include>
    <directory suffix="_test.php">src</directory>
</include>

Sources:

Upvotes: 1

hakre
hakre

Reputation: 198204

This might depend a little bit on the version of Phpunit.

I could get it to work with two more older ones (3.7.22 and 3.7.38) but had a similar problem earlier. Here is my solution:

I first had the directory configured wrong:

    <testsuites>
            <testsuite name="Default">
                    <directory>/tests</directory>
            </testsuite>
    </testsuites>

As you can see in this wrong example, it's prefixed with a slash ("/"). Removing that slash results in the following XML excerpt that is working fine then:

    <testsuites>
            <testsuite name="Default">
                    <directory>tests</directory>
            </testsuite>
    </testsuites>

Ensure the directory is not prefixed with a slash and that it exists. The existence part is crucial, because the Phpunit testrunner will not display na error message if it does not exists. You will only see that no tests are executed.

No tests executed!


Taking what you have in your question as an example:

  1. <directory>unit</directory>: It fails because the directory does not exists (you think it does, I know, but it fails if the directory does not exists, Phpunit is not lying to you).
  2. <directory>unit/api/2</directory>: If it fails, it fails because the directory does not exists (you think it does, I know, but it fails if the directory does not exists, Phpunit is not lying to you).
  3. <directory>unit/api/2/*</directory>: This will always fail because the directory does not exists. Most file-systems do not allow to use the "*" character in directory- and file-names.
  4. <directory>unit/api/2/*Test.php</directory>: Same here, this is not a valid directory name, it fails because the directory does not exists.
  5. <directory suffix="Test.php">unit/api/2</directory>: If it fails, it fails because the directory does not exists (you think it does, I know, but it fails if the directory does not exists, Phpunit is not lying to you). Additionally the suffix parameter with the value "Test.php" is superfluous because this is the default suffix. As it is superfluous, this is cruft and you should remove it from the XML file.

No software is without bugs and this naturally applies to Phpunit as well, but you should first of all consider that Phpunit is not lying to you in the first place.

I for myself had not problems with the two versions documented after I corrected the directory configuration. Your mileage may vary. This applies to the points 1, 2 and 5 specifically: From your question it looks like those directories do exist, but from the description you give and what is the Phpunit behaviour I can see, those directories do not exist.

Points 3 and 4 should be clear, they simply do not work because you put wildcards into the directory name which was meant to describe a concrete path not to hint that wildcard usage was intended / allowed. You can find this point outlined as well in a previous Q&A question: Attempting to run Phpunit with a configuration XML file results in exception.


The information in this answer is based on

  • PHPUnit 3.7.22 and 3.7.38 by Sebastian Bergmann.
  • PHP 5.4.36 by php.net

Upvotes: 0

Matthijs van den Bos
Matthijs van den Bos

Reputation: 1189

Where in you path relative to the unit directory is the phpunit.xml file?

if your path looks anything like this:

projectroot
|- phpunit.xml
|- unit/
   |- api/

You could try setting the directory to the following in phpunit.xml:

<directory>./unit</directory>

And then run phpunit from root like this: phpunit -c phpunit.xml

If that doesn't work, something else is wrong. What happens if you run this:

phpunit ./unit/api/2

If no tests are being run then, please answer the following questions:

  • maybe your methods in the testcases don't start with 'test'?
  • Do all testcase files end with Test.php?
  • Do all testcase classes extend PHPUnit_Framework_TestCase?
  • Do all testcase classnames end with 'Test'?

Upvotes: 2

Related Questions