首页 > 解决方案 > Phpunit-Selenium 运行所有的断言函数

问题描述

我想使用 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);
    }

}

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

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

我从该响应中了解到的是,不知何故,它不理解只运行那些具有 @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>

我感谢任何帮助,谢谢。

标签: phpseleniumphpunitbrowser-testing

解决方案


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

显然 phpunit-selenium 与 phpunit >= 9.4.0 不兼容。有一个未解决的问题: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


推荐阅读