我想使用 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/