Non-testable base class extending PHPUnit_Framework_TestCase

馋奶兔 提交于 2019-12-22 01:23:48

问题


Summary

How can I create a base class that extends PHPUnit_Framework_TestCase and use that for subclassing actual test cases, without having the base class itself tested by PHPUnit?

Further explanation

I have a series of related test cases for which I have created a base class that contains some common tests to be inherited by all test cases:

BaseClass_TestCase.php:
class BaseClass_TestCase extends PHPUnit_Framework_TestCase { 
  function test_common() {
    // Test that should be run for all derived test cases
  }
}

MyTestCase1Test.php:
include 'BaseClass_TestCase.php';
class MyTestCase1 extends BaseClass_TestCase {
    function setUp() {
      // Setting up
    }
    function test_this() {
      // Test particular to MyTestCase1
    }
}

MyTestCase2Test.php:
include 'BaseClass_TestCase.php';
class MyTestCase2 extends BaseClass_TestCase {
    function setUp() {
      // Setting up
    }
    function test_this() {
      // Test particular to MyTestCase2
    }
}

My problem is that when I try to run all the tests in the folder, it fails (without output).

Trying to debug I've found that the problem lies with the base class being itself a subclass of PHPUnit_Framework_TestCase and therefore PHPUnit will also try to run its tests. (Until then I naively thought that only classes defined inside actual test files - filenames ending in Test.php - would be tested.)

Running the base class as a test case out of context doesn't work due to details in my specific implementation.

How can I avoid the base class being tested, and only test the derived classes?


回答1:


Make it abstract, PHPUnit should ignore it.




回答2:


For avoiding any file being tested, you can exclude it in phpunit.xml file. In your case is <exclude>./tests/BaseClass_TestCase.php</exclude> Whole file example



来源:https://stackoverflow.com/questions/9421638/non-testable-base-class-extending-phpunit-framework-testcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!