skip to Main Content

I created a suite of php scripts, which perform a number of ‘Memcached’ operations, and I have written phpunit tests for this suite. The name of the test suite is Memcached, and the phpunit.xml.dist file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
    <testsuites>
        <testsuite name="Memcached">
            <directory>./test</directory>
        </testsuite>
    </testsuites>
</phpunit>

However, when I run this test suite with the --testsuite=Memcached flag, I receive the following error:

PHP Fatal error:  Uncaught PHPUnitFrameworkException: Class "Memcached" does not extend PHPUnitFrameworkTestCase.

The error presumably occurs because php already has a class called Memcached.

If I rename the testsuite to MemcachedTest in the XML file, and run the tests with the --testsuite=MemcachedTest flag, the unit tests run and complete with zero errors.

I would rather name the test suite Memcached, as this would match the formatting of our other test suites.

Can test suites for ‘phpunit’ be named the same as an existing class?

2

Answers


  1. Your test classes need to extend PHPUnit_Framework_TestCase class

    <?php
    /**
     * Class SomeTest.
     */
    class SomeTest extends PHPUnit_Framework_TestCase
    {
        public function testSomething()
        {
            // test case
        }
    }
    

    See the doc https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html

    Login or Signup to reply.
  2. To answer your question:

    Can test suites for ‘phpunit’ be named the same as an existing class?

    Yes, but only if the class is a test suite implementation.

    Otherwise, no.

    The reason why you’re running into this issue is:

    If the test suite name is a name of an existing class, the class will be instantiated as a test suite.

    Memcached is obviously not a PHPUnit test suite.

    On the other hand:

    If the test suite is just a string, an empty TestSuite object will be created with the given name.

    To solve your issue, give the test suite a name that’s not a class name:

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit colors="true">
        <testsuites>
            <testsuite name="Memcached Tests">
                <directory>./test</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    The behaviour you’ve experienced is actually documented in the PHPUnitFrameworkTestSuite class:

    /**
     * Constructs a new TestSuite:
     *
     *   - PHPUnitFrameworkTestSuite() constructs an empty TestSuite.
     *
     *   - PHPUnitFrameworkTestSuite(ReflectionClass) constructs a
     *     TestSuite from the given class.
     *
     *   - PHPUnitFrameworkTestSuite(ReflectionClass, String)
     *     constructs a TestSuite from the given class with the given
     *     name.
     *
     *   - PHPUnitFrameworkTestSuite(String) either constructs a
     *     TestSuite from the given class (if the passed string is the
     *     name of an existing class) or constructs an empty TestSuite
     *     with the given name.
     *
     * @param mixed  $theClass
     * @param string $name
     *
     * @throws Exception
     */
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search