Phpunit-Selenium 运行所有断言函数

标签 php selenium phpunit browser-testing

我想使用 phpunit-selenium 编写一些浏览器测试,现在这是我的测试类

<?php

namespace Tests\System;

use Tests\TestCase;

class LoginTest extends TestCase
{

    /**
     * @test
     */
    public function showLoginPage()
    {
        $this->url('/');
        $this->assertTrue(true);
    }
}

这是我为添加额外的方法和属性而创建的 testCase 类

<?php

namespace Tests;
require_once dirname(__DIR__) . '/vendor/autoload.php';
use PHPUnit\Extensions\Selenium2TestCase;

class TestCase extends Selenium2TestCase {

    public function setUp(): void
    {
        $config = new \Config('test');
        $this->setDesiredCapabilities([
            'chromeOptions'=>['w3c' => false, "args"  => [
                '--no-sandbox',
                '--disable-gpu',
                '--headless',
                '--verbose',
                '--whitelisted-ips='
            ]]]);
        $this->setHost('localhost');
        $this->setPort(4444);
        $this->setBrowserUrl('http://localhost/');
        $this->setBrowser('chrome');
        $this->shareSession(true);
    }

}

现在,当我运行供应商/bin/phpunit 时,它会运行大约 366 个测试。但它们不是测试,它们是断言类中的方法。例如它运行这个方法

Tests\System\LoginTest::objectHasAttribute
Tests\System\LoginTest::classHasStaticAttribute
Tests\System\LoginTest::classHasAttribute

我从该响应中了解到的是,不知怎的,它不明白只运行那些具有 @test 注释或前缀 test 的方法。最终文件是phpunit.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
         backupGlobals="false"
>

    <testsuites>
        <testsuite name="PHPUnit">
            <directory suffix="Test.php">Tests</directory>
        </testsuite>
    </testsuites>

</phpunit>

非常感谢您的帮助,谢谢。

最佳答案

就我而言,我使用的是 phpunit 9.5.4 和 phpunit-selenium 9.0.0。

显然 phpunit-selenium 与 phpunit >= 9.4.0 不兼容。有一个 Unresolved 问题:https://github.com/giorgiosironi/phpunit-selenium/issues/453

所以解决方案(对我来说)是将 phpunit 降级到 9.3.x:

composer require --dev phpunit/phpunit:9.3.*

显然 phpunit 8.5.9 也出现同样的问题:https://github.com/sebastianbergmann/phpunit/issues/4516

关于Phpunit-Selenium 运行所有断言函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65988431/

相关文章:

php - 组合学:构建 10 组,每组 100 个元素,同时元素保持排序

php - Wordpress,PHP,URL编码问题

javascript - WordPress 中的 AJAX 表单提交无法正常工作(续...)

python - 不可能在 Selenium 上找到永久覆盖的解决方案

testing - symfony WebTestCase 客户端不发送发布数据

php - AJAX 应用程序中的代码分离

selenium - 如何在 selenium ide 中引用/Handel 动态按钮

java - 如何单击具有特定主题的 gmail 中的未读电子邮件说 "First_name"

php - 单元测试调用具体子方法的抽象类方法

php - Symfony:更改请求基类并更新测试环境