我在phpunit中运行一系列测试,这些测试存在于单独的测试套件中,其列表由phpunit配置文件控制.当测试单独运行时(即不是通过配置文件运行,因此一次运行测试套件)它们通过但是,当一起运行时,我会失败.
仔细研究一下,问题是每个测试套件都在框架中加载(通过require_once),并且该框架根据require_once时的设置进行一些内部配置.看来,在运行phpunit配置文件中单独列出的测试套件之间,各种各样的事情仍然存在.在这种特殊情况下,框架已被视为已加载.
那么 - 有没有办法让phpunit独立执行一系列测试套件,即等同于在测试套件上一次运行一个phpunit? (phpunit正在由autotest机器上的cruisecontrol触发,并且在提交之前由开发人员在本地触发.)我尝试了诸如'--process-isolation'和'--no-globals-backup'之类的选项但没有成功.
一个说明问题的简单例子是'constant.php'文件:
<?php
if (defined('XYZZY')) define('TEST', 1);
else define('TEST', 2);
Run Code Online (Sandbox Code Playgroud)
testuite'TestOne.php':
<?php
define('XYZZY', "");
require_once('constant.php');
class TestOne extends PHPUnit_Framework_TestCase
{
public function testOne()
{
$this->assertEquals(TEST, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
一个类似的测试套件'TestTwo.php':
<?php
require_once('constant.php');
class TestTwo extends PHPUnit_Framework_TestCase
{
public function testTwo()
{
$this->assertEquals(TEST, 2);
}
}
Run Code Online (Sandbox Code Playgroud)
和一个phpunit配置文件:
<phpunit>
<testsuites>
<testsuite name="First">
<file>./TestOne.php</file>
</testsuite>
<testsuite name="Second">
<file>./TestTwo.php</file>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud) 升级到PHPUnit v> 3.5后(我忘了)我继续收到此错误,因为我导入了PHPUnit/Extensions/Database/TestCase.php:
require_once(...): failed to open stream: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我偷看了我的/ usr/share/php文件夹中的PHPUnit文件夹,结果发现它已不存在了.
事实上,查看官方源代码可以发现它似乎已被删除.
所以现在我使用的是版本3.4.15.但它并不理想,因为桌面上的断言仍然不受支持.目前我只能使用填充数据库的CLEAN_INSERT功能.
除了sleep()在我的测试中使用之外,我想知道是否有人知道在继续我的断言之前明确等待表单提交(POST)完成的更好策略.这是我的测试看起来非常精简的版本,使用phpunit和php的 web -webdriver).
function test_form_submission()
{
// setup
$web_driver = new WebDriver();
$session = $web_driver->session();
$session->open('http://example.com/login');
// enter data
$session->element('css selector', 'input[name=email]')->value(array('value' => str_split('test@example.com')));
$session->element('css selector', 'input[name=password]')->value(array('value' => str_split('testpassword')));
// click button to submit form
$session->element('css selector', 'button[name=submit]')->click();
// How do I wait here until the click() above submits the form
// so I can check that we correctly arrives at the destination below
// without resorting to sleep()?
// Confirm that login was successful because …Run Code Online (Sandbox Code Playgroud) 在JUnit上,您可以使用注释@RunWith(Parameterized.class)多次运行单个单元测试,具有不同的实际和预期结果.我是PHPUnit的新手,所以我想知道实现相同的建议方法(运行一个具有许多实际预期结果的单元测试)?
我创建了一个Symfony命令来将我的应用程序重置为初始状态.要从cli运行该命令,我需要输入:
php bin/console app:reset
Run Code Online (Sandbox Code Playgroud)
我想在所有单元测试之前运行该命令一次.我可以设法在每次测试之前完成,并且肯定在所有课程之前.因此我使用了那段代码:
public function setUp()
{
$kernel = new \AppKernel('test', true);
$kernel->boot();
$app = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$app->setAutoExit(false);
$app->run(new ArrayInput([
'command' => 'app:reset', ['-q']
]), new NullOutput());
}
Run Code Online (Sandbox Code Playgroud)
如上所述,这在每次测试之前都很好用,并且setUpBeforeClass()我可以在每个类之前使用它,但是在所有测试之前就足够了,因为该命令需要一些时间才能运行.
我正在尝试在PHP项目中使用PHPUnit.这是我的项目结构(文件是斜体字体样式)
控制器
测试
供应商
composer.json
我的文件:
composer.json
{
"require-dev": {
"phpunit/phpunit":"5.5.4"
}
}
Run Code Online (Sandbox Code Playgroud)
Pages.php
<?php
namespace controllers
class Pages
{
public function render()
{
return 'Hello World';
}
}
Run Code Online (Sandbox Code Playgroud)
pagesTest.php
<?php
class PagesTest extends PHPUnit_Framework_TestCase
{
public function testRenderReturnsHelloWorld()
{
$pages = new \controllers\Pages();
$expected = 'Hello Word';
$this->assertEquals($expected, $pages->render());
}
}
Run Code Online (Sandbox Code Playgroud)
当我打开命令行时,我写道:
C:\xampp\htdocs\PHPUnitTestProject\vendor\bin>phpunit ../../tests/PagesTest.php
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息: PHP Fatal error: Class 'controllers\Pages' not found in C:\xampp\htdocs\PHPUnitTestProject\tests\pagesTest.php on line 7
这是一个路径问题.我认为这是因为它搜索C:\xampp\htdocs\PHPUnitTestProject\vendor\bin\controllers\Pages()哪个不存在.
它应该是C:\xampp\htdocs\PHPUnitTestProject\controllers\Pages()
这似乎只发生在我的系统上.没有其他人可以重现它.如果错误的消息太大(在我的情况下大约65k字节),屏幕上不会打印任何内容.我在Windows 7上使用PHP 7.1.3,默认的php.ini(内存限制设置为8gb)和默认的PHPUnit 6.0.13配置.错误不会出现在prompt和powershell中.
<?php
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\TestCase;
class MyConstraint extends Constraint
{
protected $expected;
function __construct($expected){
parent::__construct();
$this->expected = $expected;
}
protected function matches($actual){
return false;
}
function failureDescription($other): string{
return "desc";
}
function toString(){
return "description";
}
function additionalFailureDescription($other): string{
return str_repeat("x", 100000);
// If set to a smaller dump, error will appear
// some people I asked to try could dump one million
// bytes without problems, while I can't print more
// than about 50k …Run Code Online (Sandbox Code Playgroud) 我正在为我的控制器编写一些测试,但我的一个测试不起作用.它倾向于搜索并将结果返回到页面.但它实际上是重定向到主页.这是我的代码:
use DatabaseMigrations;
protected $user;
public function setUp()
{
parent::setUp();
$this->seed();
$this->user = factory(User::class)->create(['role_id' => 3]);
}
/** @test */
public function test_manage_search_user()
{
$response = $this->followingRedirects()->actingAs($this->user)->get('/manage/users/search', [
'choices' => 'username',
'search' => $this->user->username,
]);
$response->assertViewIs('manage.users');
$response->assertSuccessful();
$response->assertSee($this->user->email);
}
Run Code Online (Sandbox Code Playgroud)
您应该使其工作的URL如下所示:
http://localhost/manage/users/search?choices=username&search=Test
Run Code Online (Sandbox Code Playgroud)
我再次检查,看起来它没有在带有get请求的参数中给出.我怎样才能解决这个问题?
我正在尝试模拟phpunit的类。php单元因错误而失败Could not load mock ... class already exists。这是我正在运行的唯一测试,因此不可能已经模拟了该类。
任何建议,将不胜感激。
这是错误情况:
namespace Tests\Feature;
use Tests\TestCase;
class DeactivateACSTest extends TestCase
{
public function testDeactivateAcs()
{
$deviceController = \Mockery::mock('overload:App\Http\Controllers\Cloud\DeviceController');
$deviceController
->shouldReceive('deactivateACS')
->andReturn('hilfehilfehilfe');
$devCon = new \App\Http\Controllers\Cloud\DeviceController();
$this->assertEquals('hilfehilfehilfe', $devCon->deactivateACS());
}
}
Run Code Online (Sandbox Code Playgroud)
在没有--code-coverage它的情况下运行时:
[13:10:15] vagrant@homestead [~/Code/ekp] $ phpunit --filter DeactivateACS
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ?
Time: 1.08 seconds, Memory: 16.00MB
OK (1 test, 3 assertions)
Run Code Online (Sandbox Code Playgroud)
但是,与--code-coverage它一起运行时失败:
[13:10:23] vagrant@homestead [~/Code/ekp] $ phpunit --coverage-html coverage --coverage-text=code_coverage.txt …Run Code Online (Sandbox Code Playgroud) 当我对运行在其上的symfony服务器执行正常请求时http://localhost:8000/api/admin/login_check,返回所需的jwt令牌.
但是,当我使用功能测试(with ./bin/phpunit)时,我收到以下错误:
错误:无法找到path \"/ api/admin/login_check \"的控制器.路由配置错误.
我还浏览了功能测试文档.
不要犹豫克隆或分叉这个项目进行测试.有README.md解释安装步骤.
我还能够通过克隆lexikjwtauthenticationbundle的创建者之一提供的工作示例来重现该bug.
运行时发生 ./bin/phpunit
[2019-01-31 09:37:49] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"http://localhost/api/admin/login_check","method":"POST"} []
[2019-01-31 09:37:49] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-01-31 09:37:49] request.WARNING: Unable to look for the controller as the "_controller" parameter is missing. [] []
Run Code Online (Sandbox Code Playgroud)
执行curl或postman请求时发生
[2019-01-29 21:16:26] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"https://localhost:8000/api/admin/login_check","method":"POST"} []
[2019-01-29 21:16:27] doctrine.DEBUG: SELECT t0.id AS id_1, t0.email …Run Code Online (Sandbox Code Playgroud)