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
Your test classes need to extend
PHPUnit_Framework_TestCase
classSee the doc https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html
To answer your question:
Yes, but only if the class is a test suite implementation.
Otherwise, no.
The reason why you’re running into this issue is:
Memcached
is obviously not a PHPUnit test suite.On the other hand:
To solve your issue, give the test suite a name that’s not a class name:
The behaviour you’ve experienced is actually documented in the
PHPUnitFrameworkTestSuite
class: