使用PHPUnit测试异常时,要求每个语句或断言必须抛出异常才能使测试通过的最佳方法是什么?
我基本上想做这样的事情:
public function testExceptions()
{
$this->setExpectedException('Exception');
foo(-1); //throws exception
foo(1); //does not throw exception
}
//Test will fail because foo(1) did not throw an exception
Run Code Online (Sandbox Code Playgroud)
我想出了以下内容,它完成了这项工作,但IMO非常难看.
public function testExceptions()
{
try {
foo(-1);
} catch (Exception $e) {
$hit = true;
}
if (!isset($hit))
$this->fail('No exception thrown');
unset($hit);
try {
foo(1);
} catch (Exception $e) {
$hit = true;
}
if (!isset($hit))
$this->fail('No exception thrown');
unset($hit);
}
Run Code Online (Sandbox Code Playgroud) 我需要做的一些测试需要将已知数组与我将要运行的函数得到的结果进行比较.
用于递归地比较数组:
我正在尝试启动PHPUnit并运行以下是我目前正在遵循的步骤:
### Install new PEAR Version needed for PHPUnit 3.X
### Download: http://pear.php.net/go-pear.phar Save it under C:\xampp\php
Open a command prompt and go to C:\xampp\php
Type "php go-pear.phar" (Installs new PEAR)
Type "pear update-channels" (updates channel definitions)
Type "pear upgrade --alldeps" (upgrades all existing packages and pear)
Type "pear channel-discover components.ez.no" (this is needed for PHPUnit)
Type "pear channel-discover pear.symfony-project.com" (also needed by PHPUnit)
Type "pear channel-discover pear.phpunit.de" (This IS phpunit)
Type "pear install --alldeps phpunit/PHPUnit" (installs PHPUnit and all …Run Code Online (Sandbox Code Playgroud) 我一直在努力让PHPunit为我的zend框架项目工作,并遇到了各种各样的问题.我重新安装了PEAR并卸载了PHPunit,并重新安装了它们.
我目前的错误是
demian @ dimbo-TP:〜$ phpunit PHP警告:require_once(PHPUnit/Framework/MockObject/Autoload.php):无法打开流:/usr/share/php/PHPUnit/Autoload.php上没有这样的文件或目录48 PHP致命错误:require_once():无法打开所需的'PHPUnit/Framework/MockObject/Autoload.php'(include_path ='.:/ usr/share/php /:/ usr/local/share/php/library')in第48行的/usr/share/php/PHPUnit/Autoload.php
我使用的是ubuntu 11.10,PHP 5.3.6-13ubuntu3.2
如果有人能给我一些指示,我将非常感激.我在谷歌上看了很多,但似乎没有什么能解决我的问题.
谢谢,
德棉.
这是我的shell转储,它显示了我最近所做的事情:
demian@dimbo-TP:~$ phpunit
The program 'phpunit' is currently not installed. You can install it by typing:
sudo apt-get install phpunit
demian@dimbo-TP:~$ sudo pear channel-discover pear.phpunit.de
[sudo] password for demian:
Channel "pear.phpunit.de" is already initialized
demian@dimbo-TP:~$ sudo pear channel-discover pear.symfony-project.com
Channel "pear.symfony-project.com" is already initialized
demian@dimbo-TP:~$ sudo pear channel-discover components.ez.no
Channel "components.ez.no" is already initialized
demian@dimbo-TP:~$ sudo pear update-channels
Updating channel "components.ez.no"
Channel …Run Code Online (Sandbox Code Playgroud) 我是一个相对较新的单元测试转换器,我遇到了一个绊脚石:
如何使用PHP的内置ftp函数测试连接到远程FTP服务器并在远程FTP服务器上执行操作的代码?一些谷歌搜索出现了Java(MockFtpServer)的快速模拟选项,但没有什么可用于PHP.
我怀疑答案可能是为PHP的ftp函数创建一个包装类,随后可以对其进行存根/模拟来模仿成功/不成功的ftp操作,但我真的很感激那些比我聪明的人的一些意见!
请注意,我一直在使用PHPUnit,并且需要专门针对该框架提供帮助.
根据@hakre的请求,我想测试的简化代码如下所示.我基本上要求最好的测试方法:
public function connect($conn_name, $opt=array())
{
if ($this->ping($conn_name)) {
return TRUE;
}
$r = FALSE;
try {
if ($this->conns[$conn_name] = ftp_connect($opt['host'])) {
ftp_login($this->conns[$conn_name], $opt['user'], $opt['pass']);
}
$r = TRUE;
} catch(FtpException $e) {
// there was a problem with the ftp operation and the
// custom error handler threw an exception
}
return $r;
}
Run Code Online (Sandbox Code Playgroud)
更新/解决方案摘要
问题摘要
我不确定如何单独测试需要与远程FTP服务器通信的方法.您如何测试能够连接到您无法控制的外部资源,对吧?
解决方案摘要
为FTP操作创建适配器类(方法:连接,ping等).然后,在测试使用适配器执行FTP操作的其他代码时,此适配器类很容易存根以返回特定值.
更新2
我最近在测试中遇到了一个使用命名空间的漂亮技巧,它允许你"模拟"PHP的内置函数.虽然适配器是我的特定情况下的正确方法,但这可能对类似情况下的其他人有所帮助:
我不时会在PHPUnit中进行风险测试.通常我可以找到风险测试的原因.但是这个任务可能很耗时,因为我没有看到来自PHPUnit的任何消息,为什么测试被标记为有风险.我只得到这样的东西:
PHPUnit 4.4.5 by Sebastian Bergmann.
Configuration read from phpunit.xml.dist
R...................R.R...
Time: 11,91 seconds, Memory: 42,50Mb
OK, but incomplete, skipped, or risky tests!
Tests: 26, Assertions: 32, Risky: 3.
Run Code Online (Sandbox Code Playgroud)
是否有任何选项告诉PHPUnit显示消息或更好的东西,如堆栈跟踪导致风险标志的代码?完整的风险测试原因列表也可能会有所帮助.
当我的开发箱(Linux Mint)上的PHPUnit测试正常失败时,它会在我的连续集成框(Centos)上导致"分段错误".两台机器都运行相同版本的PHPUnit.我的开发框运行PHP 5.3.2-1ubuntu4.9,CI为PHP 5.2.17.我宁愿放弃升级PHP作为最后的手段.
根据这个线程:PHPUnit得到分段错误 我已经尝试停用/重新安装Xdebug.我没有inclue.so安装.
在CI框中,我目前只有两个活动扩展:dom来自php-xml(phpunit需要)和memcache(我的框架需要),所有其他扩展都已关闭.
假设我有一个包含私有属性和相关公共getter和setter的类.我想用PHPUnit测试,在使用setter之后属性获取正确的值,或者getter返回正确的属性.
当然,我可以通过使用getter来测试setter,看看对象是否存储了正确的值,反之亦然,以便测试getter.但是,这并不能保证私有财产是正在设置的私有财产.
说我有以下课程.我创建了一个属性,getter和setter.但我在属性名称中输入了一个拼写错误,因此getter和setter实际上并没有操纵他们想要操纵的属性
class SomeClass
{
private
$mane = NULL; // Was supposed to be $name but got fat-fingered!
public function getName ()
{
return ($this -> name);
}
public function setName ($newName)
{
$this -> name = $newName;
return ($this);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行以下测试
public function testSetName ()
{
$this -> object -> setName ('Gerald');
$this -> assertTrue ($this -> object -> getName () == 'Gerald');
}
Run Code Online (Sandbox Code Playgroud)
我会得到一个通行证.然而,实际上发生了一件非常糟糕的事情,我不想要.当调用setName()时,它实际上在类中创建了一个新属性,其名称我认为我的私有属性,只有setter创建的属性是公共的!我可以使用以下代码演示:
$a = new SomeClass;
$a -> setName('gerald');
var_dump ($a -> getName ()); …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个非常标准的单元测试,我调用一个方法并断言它的响应,但是我正在测试的方法在同一个类中调用另一个方法,它会做一些繁重的工作.
我想模拟那个方法,但仍然执行我正在测试的方法,只有通过调用另一个方法返回的模拟值.
我愚弄了这个例子,让它变得尽可能简单.
class MyClass
{
// I want to test this method, but mock the handleValue method to always return a set value.
public function testMethod($arg)
{
$value = $arg->getValue();
$this->handleValue($value);
}
// This method needs to be mocked to always return a set value.
public function handleValue($value)
{
// Do a bunch of stuff...
$value += 20;
return $value;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试写测试.
class MyClassTest extends \PHPUnit_Framework_TestCase
{
public function testTheTestMethod()
{
// mock the object that is passed …Run Code Online (Sandbox Code Playgroud) 我收到此错误:
1) XTest::testX
array_merge(): Argument #1 is not an array
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Run Code Online (Sandbox Code Playgroud)
在这个测试案例中:
use PHPUnit\Framework\TestCase;
class XTest extends TestCase
{
function __construct()
{}
function testX()
{
$this->assertTrue(true);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我删除__construct方法,我的测试通过.PHPUnit处理我的类构造函数方法会发生什么?它在PHPUnit版本4.8中运行良好,但现在我使用PHPUnit版本6.1.3
php ×10
phpunit ×10
unit-testing ×3
constructor ×1
extends ×1
installation ×1
pear ×1
recursion ×1
white-box ×1