35

I have written an abstract test case class that is to be extended by concrete test case classes.

It extends from the PHPUnit_TestCase.

Is there a method or annotation that signals Phpunit to not execute this abstract test (but does not mark it as skipped or incomplete)?

Right now Phpunit runs the abstract test class as well and then reports an error that it can not instantiate it - which is by language: An abstract class can not be instantiated.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • is this a duplicate of http://stackoverflow.com/questions/1413959/making-phpunit-ignore-things ? – James C May 09 '11 at 08:25
  • No, that question is about command line parameters. My question is about to have it coded in independent to commandline arguments. – hakre May 09 '11 at 09:31

4 Answers4

43

Just add the skip on the setUp():

protected function setUp()
{
    $this->markTestIncomplete();
}
jhvaras
  • 2,005
  • 21
  • 16
  • 6
    That would mark all extending tests as incomplete, exactly *not* what I asked about (just in case that was not clear). – hakre Sep 16 '16 at 11:12
  • protected function setUp() :void { $this->markTestIncomplete(); } – 4n70wa Dec 22 '20 at 14:23
  • 1
    Is off-topic of original question, but in case you reach here looking for skipping temporarily, you can also use `$this->markTestSkipped('Work in progress');` to skip the test when you want to temporarily skip this class while you are refactoring other sections that this class test requires. – nelson6e65 Sep 08 '22 at 23:19
34

If it is named FooTest rename it to FooTestCase.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • 4
    I added `Case` to the abstract class name and I now only get the error when executing that test directly. Makes sense. Executing the whole directory ignores it. Thanks! – hakre May 09 '11 at 09:30
  • @hakre - Yes, if you give PHPUnit the name of a file it doesn't do any scanning on its own. – David Harkness May 10 '11 at 00:31
  • 1
    Yeah, makes sense. Thanks again, today in the morning I realized that I already used that feature for an abstract base class nearly the whole testsuite is extending from. A typical case of not seeing the forest for the trees. – hakre May 10 '11 at 07:57
  • are you talking rename class or file? i renamed class and phpunit still runs them. – S S Sep 21 '16 at 10:40
  • 2
    @SS Renaming the class itself. If you don't tell PHPUnit to run a specific file, it loads all files named `*Test.php` and then instantiates all classes named `*Test`. Make sure your abstract test cases that must be extended do not end in `Test`. – David Harkness Sep 21 '16 at 22:27
  • 1
    Yes, I see, I have bigger issue phpunit somehow running all files in /tests folder even if I completely delete innerXml of `` node as I found out later today http://stackoverflow.com/questions/39616107/ignore-unignore-tests-in-phpunit-xml – S S Sep 21 '16 at 22:37
  • 2
    @NinoŠkopac Declare the class as abstract, then it should work. Simply renaming didn't work for me too. – ob-ivan Feb 19 '18 at 09:56
16

Assuming you want to exclude the file name TestCase.php. As in my case I use this class as an abstract to all my test classes which itself extends PHPUnit_Framework_TestCase.

Solution: add this to your phpunit.xml

<testsuites>
    <testsuite name="BLABLA">
        <directory suffix=".php">./tests</directory>
        <exclude>./tests/TestCase.php</exclude>
    </testsuite>
</testsuites>

My TestCase.php example:

<?php
namespace Foo\Bar\Tests;

use Mockery as M;
use PHPUnit_Framework_TestCase as PHPUnit;

/**
 * Class TestCase is the Parent test class that every test class must extend from.
 */
class TestCase extends PHPUnit
{

    public function __construct()
    {
        parent::__construct();
    }

//...
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
  • 2
    When it's an abstract class and it's named TestCase.php , you don't need to exclude it in the XML. That code is superfluous, therefore cruft and should be removed from the XML file. I'm sorry if that wasn't clear from the title of the question only, but the question itself should make clear what this was about. You answer a different question here. – hakre May 25 '15 at 07:58
  • I though this is the exact thing you need as your questions `Mark a PHPUnit test class as to be ignored` is what appeared first on google when I was searching for `How to ignore test class in PHPUnit`. What I really want is to ignore the test class that all other test classes extend from. Because if I don't ignore it I got the error saying `no tests in this class` which is true since it just hosts common functions for all the test classes. The right solution that I found is to ignore that class from running form the `phpunit.xml` – Mahmoud Zalt May 25 '15 at 17:29
  • The title might signal that, yes, but the class is a class of type TestCase (abstract class) and not a generic test class where the tests are written in. I don't say your answer is *wrong* just wanted to point out the little difference as you answered quite late and then sometimes the answers are a little disconnected. You didin't wrote any wrong information in your answer or something similar. Just wanted to add some context. – hakre May 26 '15 at 05:49
1

Similar to what @david-harkness said, rename the class, since PHPUnit attempts to construct all classes with the suffix, Test. In our case, we went with the suffix Tester, so:

AbstractTest becomes AbstractTester.

Jacob Thomason
  • 3,062
  • 2
  • 17
  • 21