PHPUnit Selenium 服务器 - 更好/自定义错误处理?

发布于 2024-12-10 13:28:20 字数 88 浏览 1 评论 0原文

有没有办法让 PHPUnit 在发生错误后继续?例如,我有一个大型测试套件(400 多个步骤),我希望如果没有找到某个元素,它不会阻止我的脚本的其余部分继续执行。

Is there a way I can have PHPUnit just continue after an error? For example, I have a large test suite (400+ steps) and I would prefer that if say, an element is not found, it doesn't stop the rest of my script from continuing.

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

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

发布评论

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

评论(2

半枫 2024-12-17 13:28:20

我们在 Selenium 测试中做了同样的事情。您需要捕获因断言失败而引发的异常,唯一的方法是创建一个重写断言方法的自定义测试用例基类。您可以存储失败消息并在最后使用测试侦听器使测试失败。

我面前没有代码,但它非常简单。例如,

abstract class DelayedFailureSeleniumTestCase extends PHPUnit_Extension_SeleniumTestCase
{
    public function assertElementText($element, $text) {
        try {
            parent::assertElementText($element, $text);
        }
        catch (PHPUnit_Framework_AssertionFailedException $e) {
            FailureTrackerListener::addAssertionFailure($e->getMessage());
        }
    }

    ... other assertion functions ...
}

class FailureTrackerListener implements PHPUnit_Framework_TestListener
{
    private static $messages;

    public function startTest() {
        self::$messages = array();
    }

    public static function addAssertionFailure($message) {
        self::$messages[] = $message;
    }

    public function endTest() {
        if (self::$messages) {
            throw new PHPUnit_Framework_AssertionFailedException(
                    implode("\n", self::$messages));
        }
    }
}

We do the same thing in our Selenium tests. You need to catch the exceptions thrown for assertion failures, and the only way to do that is to create a custom test case base class that overrides the assertion methods. You can store the failure messages and fail the test at the end using a test listener.

I don't have the code in front of me, but it was pretty straight-forward. For example,

abstract class DelayedFailureSeleniumTestCase extends PHPUnit_Extension_SeleniumTestCase
{
    public function assertElementText($element, $text) {
        try {
            parent::assertElementText($element, $text);
        }
        catch (PHPUnit_Framework_AssertionFailedException $e) {
            FailureTrackerListener::addAssertionFailure($e->getMessage());
        }
    }

    ... other assertion functions ...
}

class FailureTrackerListener implements PHPUnit_Framework_TestListener
{
    private static $messages;

    public function startTest() {
        self::$messages = array();
    }

    public static function addAssertionFailure($message) {
        self::$messages[] = $message;
    }

    public function endTest() {
        if (self::$messages) {
            throw new PHPUnit_Framework_AssertionFailedException(
                    implode("\n", self::$messages));
        }
    }
}
请帮我爱他 2024-12-17 13:28:20

有一个更好的方法可以做到这一点。您可以只重载一个方法:runTest(),而不是重载每个 assert*() 方法。它针对每个断言,并且可以捕获异常:

abstract class AMyTestCase extends PHPUnit_Framework_TestCase
{
    public function runTest()
    {
        try {
            parent::runTest();
        }
        catch ( MyCustomException $Exc ) {
            // will continue tests
        }
        catch ( Exception $Exc ) {
            if ( false === strpos($Exc->getMessage(), 'element not found') ) {
                // rethrow:
                throw $Exc;
            }
            // will also continue
        }
    }
}

There is a better way to do this. Instead of overloading every assert*() method you can overload just one method: runTest(). It is for each assertion, and exceptions can be caught:

abstract class AMyTestCase extends PHPUnit_Framework_TestCase
{
    public function runTest()
    {
        try {
            parent::runTest();
        }
        catch ( MyCustomException $Exc ) {
            // will continue tests
        }
        catch ( Exception $Exc ) {
            if ( false === strpos($Exc->getMessage(), 'element not found') ) {
                // rethrow:
                throw $Exc;
            }
            // will also continue
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文