我仍然在研究应该使用哪种工具.当我四处寻找时,我注意到他们中没有一个真的有任何新版本:
Xinc
Version 2.0.1 released 02/05/08
Phing
Version 2.3.3 released 12/07/08
phpUnderControl
Version 0.4.4 released 08/09/08
Run Code Online (Sandbox Code Playgroud)
我应该选择phpUnderControl,因为它有最新版本,开发可能还会继续吗?我不想仅仅因为php 5.3附带的某些功能无法由其中一个处理而在半年内切换.
我需要PHP Unit,SVN和Codesniffer支持.
我试图在PHP和PHPUnit中创建一个模拟对象.到目前为止,我有这个:
$object = $this->getMock('object',
array('set_properties',
'get_events'),
array(),
'object_test',
null);
$object
->expects($this->once())
->method('get_events')
->will($this->returnValue(array()));
$mo = new multiple_object($object);
Run Code Online (Sandbox Code Playgroud)
忽略了我那些可怕的不明确的对象名称,我明白我所做的是
- 创建一个模拟对象,配置2个方法,
- 配置'get_events'方法返回一个空白数组,并
- 将模拟放入构造函数.
我现在要做的是配置第二种方法,但我找不到任何解释如何做的事情.我想做点什么
$object
->expects($this->once())
->method('get_events')
->will($this->returnValue(array()))
->expects($this->once())
->method('set_properties')
->with($this->equalTo(array()))
Run Code Online (Sandbox Code Playgroud)
或者其他一些,但这不起作用.我该怎么办?
切线,这是否表明我的代码结构很差,如果我需要配置多个方法来测试?
我想为函数库文件运行单元测试...
也就是说,我没有一个类,它只是一个带有辅助函数的文件...
例如,我在〜/ www/test创建了一个php项目
和一个文件〜/ www/test/lib/format.php
<?php
function toUpper( $text ) {
return strtoupper( $text );
}
function toLower( $text ) {
return strtolower( $text );
}
function toProper( $text ) {
return toUpper( substr( $text, 0, 1 ) ) . toLower( substr( $text, 1) );
}
?>
Run Code Online (Sandbox Code Playgroud)
工具 - >创建PHPUnit测试给出了以下错误:
Sebastian Bergmann的PHPUnit 3.4.5.
在"/home/sas/www/test/lib/format.php"中找不到类"格式".
现在,如果我编码(手工!)文件〜/ www/test/tests/lib/FormatTest.php
<?php
require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../../lib/format.php';
class FormatTest extends PHPUnit_Framework_TestCase {
protected function setUp() {}
protected function tearDown() {}
public function testToProper() {
$this->assertEquals( …Run Code Online (Sandbox Code Playgroud) 假设您创建的应用程序尝试尽可能接近地将字母A中的内容音译成字母B.
因为语言B非常复杂,所以这并不总是成功的.但你确实得到了大致的音译.
在这种情况下,如果您预计20-30%会失败,您将如何构建单元测试?
我无法让phpunit在PhpStorm的Symfony项目中phpunit -c app工作- 在OSX终端中工作正常.
这是错误:
Unable to attach test reporter to test framework of test framework quit unexpectedly
/Applications/MAMP/bin/php/php5.4.4/bin/php/private/var/folders/4l/hw8g4qlj6nnc37lfkc6hcj7w0000gn/T/ide-phpunit.php --bootstrap
/Users/greg/Repos/MyApp/app/bootstrap.php.cache --configuration
/Users/greg/Repos/MyApp/app/phpunit.xml.dist
MyApp\MyBundle\Tests\Controller\MyControllerTest
/Users/greg/Repos/MyApp/src/HvH/MyBundle/Tests/Controller/MyControllerTest.php
Testing started at 11:45 AM ...
Process finished with exit code 255
Run Code Online (Sandbox Code Playgroud)
编辑:以下是PHP日志中的错误:
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'The "app/" directory does not exist.' in /Users/greg/Repos/MyApp/vendor/symfony/symfony/src/Symfony/Component/Finder/Finder.php:650
Run Code Online (Sandbox Code Playgroud) 我一直收到以下错误:
There was 1 error:
1) Caremonk\MainSiteBundle\Tests\Controller\SecurityControllerFunctionalTest::testIndex
InvalidArgumentException: The current node list is empty.
Run Code Online (Sandbox Code Playgroud)
但是当我导航到localhost/login时,我的表单会填充正确的内容.这条线说......
$form = $crawler->selectButton('login')->form();
Run Code Online (Sandbox Code Playgroud)
导致错误.我的考试有什么问题?
功能测试:
<?php
namespace Caremonk\MainSiteBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SecurityControllerFunctionalTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/login');
$form = $crawler->selectButton('login')->form();
$form['username'] = 'testActive';
$form['password'] = 'passwordActive';
}
}
Run Code Online (Sandbox Code Playgroud)
树枝视图:
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('caremonk_mainsite_login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" …Run Code Online (Sandbox Code Playgroud) 我们有Laravel 5控制器方法:
public function getInput()
{
$input = \Request::all();
$links = $input['links'];
$this->startLinks = explode("\n", $links);
return $this;
}
Run Code Online (Sandbox Code Playgroud)
我们如何测试这种方法?如何将带有数据的POST请求传递给此方法?以及如何在我的测试方法中创建此控制器类的实例?
我注意到当我的笔记本电脑连接到互联网时,我的PHPUnit测试需要大约90秒~200秒完成.但是当我从互联网断开它时,它会在不到20秒的时间内运行!这让我感到快乐和悲伤!
在这两种情况下,所有测试都在通过,我确信我正在嘲笑对外部API的每个请求.
我正在使用Laravel和MySQL进行实际数据存储,并在测试环境中使用内存中的sqlite.我的开发环境也全部在Docker上运行.
这是与PHPUnit或我的代码有关的东西!! 任何人都知道发生了什么.谢谢
更多信息
我正在使用的域名是something.dev我的API的用途api.something.dev.每个测试至少对每个API端点进行一次调用.
DNS! 如果您认为这是由于DNS查找:我将所有域和子域更改为127.0.0.1只是为了测试它,并且它没有帮助测试仍然很慢.这应该消除DNS查找的可能性!
此外,我尝试使用PHPUnit Bridge和PHPUnit 来模拟 DNS ,但我想由于缺少文档而无法使其工作,所以我不知道DnsMock::withMockedHosts([here!!])在从我的setUp()函数调用它之后要传递什么作为参数 .
别的东西 我想是因为延迟之前和查询数据库,主要是为了存储数据后,会发生的问题是关系到数据存储.
对于在https://www.gitlab.com上托管的项目,我想在CI设置中设置代码覆盖率,因此它可以显示在作业列表中
我的配置如下所示:
.gitlab-ci.yml
image: php:7.1.1
cache:
paths:
- vendor/
before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq
# Install composer
- curl -sS https://getcomposer.org/installer | php
# Install all project dependencies
- php composer.phar install
# Run our tests
test:
only:
- master
- develop
script:
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
Run Code Online (Sandbox Code Playgroud)
作业成功,但显示错误消息
错误:没有可用的代码覆盖率驱动程序
我已更新测试覆盖率解析的设置并将正则表达式设置为
^\s*Lines:\s*\d+.\d+\%
Run Code Online (Sandbox Code Playgroud)
PHP/PHPUnit的示例.
当我运行命令
vendor/bin/phpunit --coverage-text --colors=never
Run Code Online (Sandbox Code Playgroud)
在本地,我得到以下输出:
Code Coverage Report: …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×9
unit-testing ×4
testing ×3
symfony ×2
connection ×1
gitlab ×1
gitlab-ci ×1
laravel ×1
laravel-5 ×1
netbeans ×1
performance ×1
phing ×1
phpstorm ×1
xinc ×1