扩展 PHPUnit

编程入门 行业动态 更新时间:2024-10-24 22:29:31
本文介绍了扩展 PHPUnit_Framework_TestCase 的不可测试基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何创建一个扩展 PHPUnit_Framework_TestCase 的基类并将其用于子类化实际测试用例,而不需要 PHPUnit 对基类本身进行测试?

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?

我有一系列相关的测试用例,为此我创建了一个基类,其中包含一些要被所有测试用例继承的通用测试:

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

尝试调试我发现问题在于基类本身是 PHPUnit_Framework_TestCase 的子类,因此 PHPUnit 也会尝试运行其测试.(在那之前,我天真地认为只有在实际测试文件中定义的类 - 以 Test.php 结尾的文件名 - 才会被测试.)

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?

推荐答案

让它抽象,PHPUnit 应该忽略它.

Make it abstract, PHPUnit should ignore it.

更多推荐

扩展 PHPUnit

本文发布于:2023-06-03 12:16:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHPUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!