问题描述
我想使用 PHPunit-selenium 编写一些浏览器测试,现在这是我的测试类
<?PHP
namespace Tests\System;
use Tests\TestCase;
class LoginTest extends TestCase
{
/**
* @test
*/
public function showLoginPage()
{
$this->url('/');
$this->assertTrue(true);
}
}
<?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 个测试。但它们不是测试,它们是 assert 类中的方法。例如它运行这个方法
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 不兼容。有一个未解决的问题: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