将 PHPUnit 测试类标记为被忽略

发布于 2024-11-05 13:19:43 字数 207 浏览 2 评论 0原文

我编写了一个抽象测试用例类,该类将通过具体测试用例类进行扩展。

它从 PHPUnit_TestCase 扩展而来。

是否有方法或注释指示 Phpunit 不执行此抽象测试(但不将其标记为已跳过或不完整)?

现在 Phpunit 也运行抽象测试类,然后报告无法实例化它的错误 - 这是通过语言来实现的:抽象类无法实例化。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

澜川若宁 2024-11-12 13:19:43

只需在 setUp() 上添加跳过即可:

protected function setUp()
{
    $this->markTestIncomplete();
}

Just add the skip on the setUp():

protected function setUp()
{
    $this->markTestIncomplete();
}
我三岁 2024-11-12 13:19:43

如果其名称为 FooTest,则将其重命名为 FooTestCase

If it is named FooTest rename it to FooTestCase.

酒几许 2024-11-12 13:19:43

假设您要排除文件名 TestCase.php
在我的例子中,我使用这个类作为所有测试类的抽象,它本身扩展了 PHPUnit_Framework_TestCase。

解决方案:将此添加到您的phpunit.xml

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

我的TestCase.php示例:

<?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();
    }

//...

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();
    }

//...
吻安 2024-11-12 13:19:43

与 @david-harkness 所说的类似,重命名该类,因为 PHPUnit 尝试构造带有后缀 Test 的所有类。在我们的例子中,我们使用了后缀 Tester,因此:

AbstractTest 变为 AbstractTester

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文