【发布时间】:2021-07-20 01:45:59
【问题描述】:
我已经有几个包含表单或 URL 测试的文件。但是,对于需要用户登录的测试(我使用了 Symfony 的预定义 loginUser 函数),它们不会被检测到。当我在终端上使用命令“php bin / phpunit”时,测试的数量并没有增加,这些测试没有考虑在内。请问我该怎么办? 例如,下面是测试个人资料页面 URL 的代码:
<?php
namespace App\tests\Controller;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ProfilControllerTest extends WebTestCase
{
public function testMyProfil()
{
$client = static::createClient();
$userRepository = static::$container->get(UserRepository::class);
//retrieve the test user (in the fixtures)
$testUser = $userRepository->findOneByEmail('Alex@gmail.com');
//simulate $testUser being logged in
$client->loginUser($testUser);
// test the profile page
$client->request('GET', '/monProfil');
$this -> assertEquals ( 200 , $client -> getResponse () -> getStatusCode ());
}
}
这是一个检测到的 PHPUnit 测试示例,它也是一个 URL 测试,但不需要登录:
<?php
namespace App\tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AboutControllerTest extends WebTestCase
{
public function testAbout()
{
$client = static::createClient();
//teste the page "about us"
$client->request('GET', '/aboutUs');
$this -> assertEquals ( 200 , $client -> getResponse () -> getStatusCode ());
}
}
【问题讨论】:
-
我们能看到一个实际检测比较的测试示例吗?您也可以尝试使用此命令
vendor\bin\phpunit这是我使用的命令 -
这个命令在我的终端上不起作用,错误是“bash: vendorbinphpunit: command not found”。我在我的问题中添加了一个示例。
-
测试没有运行,所以你的意思是它没有被 phpunit 检测到,对吗?你能检查你的
phpunit.xsd文件吗?查看过滤器>白名单>排除标签 -
是的,它没有被 phpunit 检测到。在我的 phpunit.xml.dist 中:
<whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">src</directory> </whitelist> -
您确定在项目的根目录中,运行
vendor\bin\phpunit出错了吗?我问是因为你写了bash : vendorbinphpunit:...
标签: php unit-testing symfony testing phpunit