41

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.

2
  • is this a duplicate of stackoverflow.com/questions/1413959/… ? Commented May 9, 2011 at 8:25
  • No, that question is about command line parameters. My question is about to have it coded in independent to commandline arguments. Commented May 9, 2011 at 9:31

4 Answers 4

49

Just add the skip on the setUp():

protected function setUp() { $this->markTestIncomplete(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

That would mark all extending tests as incomplete, exactly not what I asked about (just in case that was not clear).
protected function setUp() :void { $this->markTestIncomplete(); }
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.
38
Answer recommended by PHP Collective

If it is named FooTest rename it to FooTestCase.

7 Comments

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 - Yes, if you give PHPUnit the name of a file it doesn't do any scanning on its own.
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.
@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.
@NinoŠkopac Declare the class as abstract, then it should work. Simply renaming didn't work for me too.
|
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(); } //... 

3 Comments

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.
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
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.