我在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功能.
我正试图phpUnit在一个运行plesk pannel的centos上运行.
我已经成功安装了phpunit.
Package Version State
File_Iterator 1.3.1 stable
PHPUnit 3.6.10 stable
PHPUnit_MockObject 1.1.1 stable
PHP_CodeCoverage 1.1.2 stable
PHP_Invoker 1.1.0 stable
PHP_Timer 1.0.2 stable
PHP_TokenStream 1.1.3 stable
Text_Template 1.1.1 stable
Run Code Online (Sandbox Code Playgroud)
但是每当我从shell运行phpunit命令时,我都会收到以下错误:
PHP警告:require_once(File/Iterator/Autoload.php):无法打开流:第45行/usr/share/pear/PHPUnit/Autoload.php中没有此类文件或目录
PHP致命错误:require_once():打开失败第45行的/usr/share/pear/PHPUnit/Autoload.php中需要'File/Iterator/Autoload.php'(include_path =':')
有谁知道如何解决问题?
这是我的情况.我正在开发Zend Framework 2应用程序.我正在使用Doctrine模块与MySQL数据库进行通信.它在应用程序中工作正常,我可以从我的控制器内的服务定位器加载实体管理器.
但是在我的控制器单元测试中,服务定位器不存在,因此处理数据库的所有测试都失败,并显示如下错误消息:
致命错误:
get()在第19行的/Users/richardknop/Projects/myproject/module/Api/src/Api/Controller/UserController.php中调用非对象的成员函数
我把问题缩小到这个方法:
$this->getServiceLocator()
Run Code Online (Sandbox Code Playgroud)
哪个适用于我的控制器,但在单元测试中返回NULL.
这是我的application.config.php:
<?php
return array(
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'Api',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
Run Code Online (Sandbox Code Playgroud)
我的local.php文件包含数据库连接详细信息:
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'mydb',
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
在我的module.config.php中我有:
'doctrine' => array( …Run Code Online (Sandbox Code Playgroud) 除了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) 我是phpunit的新手并且已经阅读了关于模拟对象的文档,但目前还不是很清楚.
我正在尝试编写一个简单的测试,在类中调用断言方法.使用以下代码,我测试当调用Client :: exchangeArray时,调用Client :: getInputFilter.
class Client implements InputFilterAwareInterface
{
public function getInputFilter() {
if(!$this->_inputFilter){
$inputFactory = new InputFactory();
$inputFilter = new InputFilter();
$inputFilter->add($inputFactory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array(
'name' => 'Int'
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
),
array(
'name' => 'StripNewLines'
),
array(
'name' => 'Alpha'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' …Run Code Online (Sandbox Code Playgroud) 在JUnit上,您可以使用注释@RunWith(Parameterized.class)多次运行单个单元测试,具有不同的实际和预期结果.我是PHPUnit的新手,所以我想知道实现相同的建议方法(运行一个具有许多实际预期结果的单元测试)?
我正在尝试在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()
我正在尝试模拟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) phpunit ×10
php ×7
unit-testing ×2
doctrine-orm ×1
fatal-error ×1
junit ×1
methods ×1
mocking ×1
php-7.0 ×1
selenium ×1
symfony4 ×1