简体   繁体   English

扩展PHPUnit_Framework_TestCase的不可测试的基类

[英]Non-testable base class extending PHPUnit_Framework_TestCase

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? 如何创建扩展PHPUnit_Framework_TestCase的基类并将其用于子类化实际测试用例,而不需要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. 试图调试我发现问题在于基类本身是PHPUnit_Framework_TestCase的子类,因此PHPUnit也会尝试运行它的测试。 (Until then I naively thought that only classes defined inside actual test files - filenames ending in Test.php - would be tested.) (在那之前,我天真地认为只有在实际测试文件中定义的类 - 以Test.php结尾的文件名 - 才会被测试。)

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? 如何避免测试基类,只测试派生类?

使它抽象,PHPUnit应该忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM