我有一个Zend Framework项目,并希望使用单元测试来测试它.
在tests文件夹中,我有phpunit.xml以下内容:
<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
<directory>./</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
<file>../application/Bootstrap.php</file>
<file>../application/controllers/ErrorController.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/reprot" charset="UTP-8"
yui="true" highlight = "true" lowUpoerBound="50" highLowerBound="80"/>
<log type="textdox" target="./log/testdox.html" />
</logging>
Run Code Online (Sandbox Code Playgroud)
我bootstrap.php在/ tests/application文件夹中有如下内容:
<?php
error_reporting(E_ALL | E_STRICT);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path …Run Code Online (Sandbox Code Playgroud) 我已经创建了自己的codeniffer规则标准.他们工作正常.现在我想使用PHP UnitTest测试代码的不同规则.PhpCodesniffer已经有了PHPUnit测试用例的框架.
所以使用它我已经扩展了他们的AbstractSniffUnitTest单元测试类
Standards/TestRules/Tests/Function/FunctionUnitTest.php和要测试的脚本Standards/TestRules/Tests/Function/FunctionUnitTest.inc.
但是当我要按命令运行脚本时phpunit PEAR\PHP\tests\AllTests.php,它会出现以下错误.
PHPUnit 3.5.14 by Sebastian Bergmann.
......................................F
Time: 6 seconds, Memory: 10.00Mb
There was 1 failure:
1) TestRules_Tests_Function_FunctionUnitTest::getErrorList
An unexpected exception has been caught: Source file C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc does not exist
C:\Program Files\PHP\PEAR\PHP\tests\Standards\AbstractSniffUnitTest.php:138
C:\Program Files\PHP\PEAR\PHP\tests\TestSuite.php:48
FAILURES!
Tests: 39, Assertions: 146, Failures: 1.
Warning: Deprecated PHPUnit features are being used 2 times!
Use --verbose for more information.
Run Code Online (Sandbox Code Playgroud)
它给出了FunctionUnitTest.inc在给定位置找不到的错误文件.我已经给予文件夹的完全权限,也验证了路径和文件位置,但它给出了同样的错误.我也在linux机器上测试了它,但它给出了同样的错误.
这是我的代码问题还是代码单元测试框架问题?
这不是一个问题,而是试图在PHPUnit上浪费我的其他时间.
我的问题是我的模拟对象,当在依赖测试中使用时,没有返回预期值.看来,PHPUnit的不保留相关测试之间的同一个对象,即使语法使它看起来像它.
有谁知道为什么PHPUnit这样做?这是一个错误吗?PHPUnit中的这类内容使得使用起来非常令人沮丧.
<?php
class PhpUnitTest
extends PHPUnit_Framework_TestCase
{
private $mock;
public function setUp()
{
$this->mock = $this->getMock('stdClass', array('getFoo'));
$this->mock->expects( $this->any() )
->method('getFoo')
->will( $this->returnValue( 'foo' ) );
}
public function testMockReturnValueTwice()
{
$this->assertEquals('foo', $this->mock->getFoo());
$this->assertEquals('foo', $this->mock->getFoo());
return $this->mock;
}
/**
* @depends testMockReturnValueTwice
*/
public function testMockReturnValueInDependentTest($mock)
{
/* I would expect this next line to work, but it doesn't! */
//$this->assertEquals('foo', $mock->getFoo());
/* Instead, the $mock parameter is not the same object as
* generated by …Run Code Online (Sandbox Code Playgroud) 我目前正在为Symfony2开发一个开源软件包,并且真的希望它在单元测试覆盖率和一般可靠性方面成为狗nadgers,但是由于我缺乏PHPUnit知识(或者复杂的场景,谁知道)..
目前,我有一个Mailer类,用于处理个别邮件方案.看起来有点像这样:
<?php
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\RouterInterface;
class Mailer
{
protected $mailer;
protected $router;
protected $templating;
protected $parameters;
public function __construct($mailer, RouterInterface $router, EngineInterface $templating, array $parameters)
{
$this->mailer = $mailer;
$this->router = $router;
$this->templating = $templating;
$this->parameters = $parameters;
}
}
Run Code Online (Sandbox Code Playgroud)
很简单,在那里得到了一些Symfony2接口gubbins来处理不同的路由和模板系统,快乐的快乐快乐.
这是我尝试为上述设置的初始测试:
<?php
use My\Bundle\Mailer\Mailer
class MailerTest extends \PHPUnit_Framework_TestCase
{
public function testConstructMailer
{
$systemMailer = $this->getSystemMailer();
$router = $this->getRouter();
$templatingEngine = $this->getTemplatingEngine();
$mailer = new Mailer($systemMailer, $router, $templatingEngine, array());
}
protected function getSystemMailer()
{
$this->getMock('SystemMailer', array('send');
} …Run Code Online (Sandbox Code Playgroud) 当我尝试phpunit .从项目的tests文件夹中运行时出现以下错误:
PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/share/php/PHPUnit/Framework.php on line 46
Run Code Online (Sandbox Code Playgroud)
我通过以下命令安装了PHPUnit:
sudo pear channel-discover pear.symfony-project.com
sudo pear channel-discover components.ez.no
sudo pear install --alldeps phpunit/PHPUnit
Run Code Online (Sandbox Code Playgroud)
因为其他方法似乎都不起作用,包括apt-get.
我认为 CodeCoverage在某个时间点改变了他们的单例模式,因此被删除getInstance但我不知道如何解决这个错误.如何降级CodeCoverage或升级PHPUnit?
我尝试通过以下命令手动安装所有内容的最新版本:
sudo apt-get install git
mkdir phpunit && cd phpunit
git clone git://github.com/sebastianbergmann/phpunit.git
git clone git://github.com/sebastianbergmann/dbunit.git
git clone git://github.com/sebastianbergmann/php-file-iterator.git
git clone git://github.com/sebastianbergmann/php-text-template.git
git clone git://github.com/sebastianbergmann/php-code-coverage.git
git clone git://github.com/sebastianbergmann/php-token-stream.git
git clone git://github.com/sebastianbergmann/php-timer.git
git clone git://github.com/sebastianbergmann/phpunit-mock-objects.git
git clone git://github.com/sebastianbergmann/phpunit-selenium.git
git clone git://github.com/sebastianbergmann/phpunit-story.git
git …Run Code Online (Sandbox Code Playgroud) 我正在尝试为一个模型编写测试,该模型包含一些普通验证器和一个使用实体管理器和请求的自定义验证器.如果由于某种原因这很重要,我正在使用phpunit进行测试.
我通过对实体管理器和请求进行存根,然后验证某些对象来测试另一个测试中的自定义验证器.由于这证明自定义验证有效,我只需要测试正常验证,如果可以,只需将自定义验证器保留.
这是我的模型:
/**
* @MyAssert\Client()
*/
abstract class BaseRequestModel {
/**
* @Assert\NotBlank(message="2101")
*/
protected $clientId;
/**
* @Assert\NotBlank(message="2101")
*/
protected $apiKey;
// ...
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我得到验证器,创建一个对象,然后验证它.
$validator = ValidatorFactory::buildDefault()->getValidator();
$requestModel = new RequestModel();
$errors = $validator->validate($requestModel);
Run Code Online (Sandbox Code Playgroud)
当然这会失败,因为它找不到为MyAssert\Client定义的Validator,它是一个服务,需要通过某个依赖注入容器来解析.
任何人都知道如何存根自定义验证器或将其从验证中排除?
我正在尝试测试我雄辩的模型,但我的测试仍然失败了"Class'Eloquent'not found'错误.如果我添加一个使用我的雄辩模型的路线并简单地打印存储在数据库中的一些信息,一切正常.只有在尝试运行phpunit时,才会发现eloquent没有被发现的问题.我的模型在应用程序/模型中,所以它应该包含在作曲家类图中,我已经完成了composer dump-autoload.我敢肯定我忽略了一些非常明显但我无法理解的东西.知道问题是什么吗?
我的测试:
class GameTest extends TestCase {
public function setUp(){
$this->game = Game::find(1);
}
public function testGameInstance(){
$this->assertInstanceOf('Game', $this->game);
}
}
Run Code Online (Sandbox Code Playgroud)
我的模特:
class Game extends Eloquent{
protected $table = 'gm_game';
protected $primaryKey = 'game_id';
}
Run Code Online (Sandbox Code Playgroud) 我已经通过PEAR安装了PHPUnit,并且我已经安装了WordPress插件测试(https://github.com/tierra/wordpress-plugin-tests)来测试我正在开发的WordPress插件.
测试运行正常的问题,我得到以下输出:
Running as single site... To run multisite, use -c multisite.xml
Not running ajax tests... To execute these, use --group ajax.
PHPUnit 3.7.21 by Sebastian Bergmann.
Configuration read from E:\LocalWebServer\dch\c\my-wp-installtion.dch\wordpress-test\wordpress\wp-content\plugins\myplugin\phpunit.xml
[41;37mF[0m.[36;1mS[0m
Time : 1 second, Memory: 30.50Mb
There was 1 failure:
1) CDOAjax_Tests::test_tests
Failed asserting that false is true.
E:\LocalWebServer\dch\c\my-wp-installtion.dch\wordpress-test\wordpress\wp-content\plugins\myplugin\Tests\test_CDOAjax_tests.php:7
[37;41m[2KFAILURES!
[0m[37;41m[2KTests: 3, Assertions: 2, Failures: 1, Skipped: 1.
[0m[2K
Run Code Online (Sandbox Code Playgroud)
我不知道这是否有帮助,但phpunit.xml包含以下内容:
<phpunit
bootstrap="bootstrap_tests.php"
backupGlobals="false"
colors="true"
>
<testsuites>
<!-- Default test suite to run all tests -->
<testsuite …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的Silex应用程序设置单元测试,但我不断收到此错误消息:
RuntimeException:根据http://symfony.com/doc/current/book/testing.html#your-first-functional-test在phpunit.xml中设置KERNEL_DIR 或覆盖WebTestCase :: createKernel()方法.
这是我的./app/phpunit.xml.dist:
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="phpunit_bootstrap.php"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/Acme/*/Tests</directory>
</testsuite>
</testsuites>
<!--<php>-->
<!--<server name="KERNEL_DIR" value="/var/www/acme/api/app/" />-->
<!--</php>-->
</phpunit>
Run Code Online (Sandbox Code Playgroud)
这是我的./app/phpunit_bootstrap.php(包括作曲家的自动加载器):
<?php
if (!@include __DIR__ . '/../../vendor/autoload.php') {
die(<<<'EOT'
You must set up the project dependencies, run the following commands:
wget http://getcomposer.org/composer.phar
php composer.phar install
EOT
);
}
Run Code Online (Sandbox Code Playgroud)
我的目录结构如下:

它看起来像是phpunit在找,*Kernel.php但我不知道为什么.
这是我的单元测试: …
由于自动加载器无法解析Doctrine\ORM\Mapping\Table,因此我很难对作曲家进行自动加载.对于Unittests,我创建了带有典型Annotations的doctrine实体类:
<?php
namespace OmniSearchTest\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Picture
*
* @ORM\Table(name="picture")
* @ORM\Entity
*/
class Picture
{
Run Code Online (Sandbox Code Playgroud)
并使用此实体创建了一个新的实体管理器.但我收到消息:
Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\Table" in class OmniSearchTest\Entity\Picture does not exist, or could not be auto-loaded.
Run Code Online (Sandbox Code Playgroud)
对于一些Unittests
首先,我有以下项目结构:
/src
/OmniSearch
SomeClass.php
/tests
/OmniSearchTest
SomeClassTest.php
/composer.json
/phpunit.xml.dist
Run Code Online (Sandbox Code Playgroud)
我的composer.json看起来像这样:
{
/* ... */
"require": {
"php": ">=5.4",
"doctrine/orm": "2.*"
},
"require-dev": {
"phpunit/phpunit": "4.*"
},
"autoload": {
"psr-0": {
"OmniSearch\\": "src/"
}
},
"autoload-dev": {
"psr-0": {
"OmniSearchTest\\": "tests/"
} …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×6
unit-testing ×4
symfony ×2
autoload ×1
codesniffer ×1
composer-php ×1
depends ×1
doctrine-orm ×1
eloquent ×1
laravel ×1
laravel-4 ×1
mocking ×1
silex ×1
tdd ×1
ubuntu-11.10 ×1
windows ×1