我想将我的PHPUnit 3.4安装降级到3.3.我只是不确定该怎么做.
如何使用PEAR在Ubuntu上安装3.3版的PHPUnit?
我开始编写PHPUnit测试,我希望测试从开发人员机器以及我们的服务器运行.开发人员的计算机设置与服务器不同,甚至彼此不同.
要在这些不同的地方运行,似乎运行测试的人必须指出它在哪里运行.然后,测试可以查找正在运行的机器的正确配置.
我想象的是:
phpunit.bat -X johns_laptop unittest.php
或者在alpha服务器上:
phpunit -X alpha unittest.php
在测试中,我将能够获得"X"(或它是什么)参数的值,并且知道,例如,应用程序根目录的路径是什么.
它看起来不像命令行允许的那样 - 或者我错过了什么?
阅读并接受单元测试,试图理解下面的帖子,解释了静态函数调用的艰辛.
我不清楚这个问题.我一直认为静态函数是一种在类中舍入实用函数的好方法.例如,我经常使用静态函数调用来初始化,即:
Init::loadConfig('settings.php');
Init::setErrorHandler(APP_MODE);
Init::loggingMode(APP_MODE);
// start loading app related objects ..
$app = new App();
Run Code Online (Sandbox Code Playgroud)
//阅读帖子后,我现在的目标是......
$init = new Init();
$init->loadConfig('settings.php');
$init->loggingMode(APP_MODE);
// etc ...
Run Code Online (Sandbox Code Playgroud)
但是,我为这堂课写的几十个测试是一样的.我什么都没改变,他们仍然都过去了.难道我做错了什么?
该职位的作者陈述如下:
静态方法的基本问题是它们是过程代码.我不知道如何对程序代码进行单元测试.单元测试假设我可以单独实例化我的应用程序.在实例化期间,我使用mocks/friendlies连接依赖项,这取代了真正的依赖项.使用程序编程没有任何"连线",因为没有对象,代码和数据是分开的.
现在,我从帖子中了解到静态方法创建了依赖关系,但是没有直观地理解为什么人们不能像常规方法那样容易地测试静态方法的返回值?
我将避免使用静态方法,但我想知道WHEN静态方法是否有用,如果有的话.从这篇文章看来,静态方法与全局变量一样邪恶,应尽可能避免.
关于该主题的任何其他信息或链接将不胜感激.
我创建了这个对象数组:
$ad_1 = new AdUnit(array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123'));
$ad_2 = new AdUnit(array('id' => '2', 'name' => 'Ad_2', 'description' => 'good ad', 'code' => 'delta', 'widget_id' => '456'));
$ad_3 = new AdUnit(array('id' => '3', 'name' => 'Ad_3', 'description' => 'bad ad', 'code' => 'sigma', 'widget_id' => '789'));
$adUnitArr = array($ad_1, $ad_2, $ad_3);
Run Code Online (Sandbox Code Playgroud)
我想检查一个函数中存在的随机广告是否存在于数组中.获取广告的代码如下所示:
$fixture = new AdGroup();
$fixture->setAds($adUnitArr);
$randad = $fixture->getRandomAd();
Run Code Online (Sandbox Code Playgroud)
现在我想检查数组是否包含我收到的随机广告,我能够做到这样:
$this->assertEquals(in_array($randad, $adUnitArr), 1); //check if ad in …Run Code Online (Sandbox Code Playgroud) 我在单元测试领域越来越深入.
我遇到的一个问题,就是我想要反馈的地方,就是当一个人运行多个测试套件时,也许只是我,但我需要使用参数--process-isolation来传递我的测试.我可以单独运行我的任何套件而没有任何问题,但是如果我在没有--process-isolation的情况下运行,那么运行我迄今为止在其中传播的180个断言的6-7套件会失败.问题是使用此参数使测试运行持续35分钟,而通常的2.5分钟.这是一个漫长的等待.
问题与使用模拟DI容器进行特定测试有关,并且当测试套件运行链接时,容器未正确重新初始化.在DI-Container上设置的静态属性用于测试预期的故障,使得以下套件中的测试失败.容器有一个参数,可以将包含的对象保存在静态var中,以便在每次调用时返回相同的实例.伪装的单身人士.这在应用程序级别上运行良好,这对测试来说只是一个麻烦.
我可以避免使用该容器参数并将应用程序编码为不使用静态属性,但是为了方法而避免使用有用的语言构造似乎有些过分.
也许我做错了什么(我当然希望如此!)但是我有一个印象,如果一个人想要在每个测试中以干净的状态运行SUT测试,那么就没有使用--process-isolation.这使得测试非常耗时并且可以稍微消除它的乐趣.我在编码时单独运行套件和测试,并在主要提交之前在后台运行套件,从而绕过了这个问题.
我正在经历的是正常的,有没有办法对付这个?你如何测试那里确保测试时间合理?如何处理静力学以免影响测试?
任何见解赞赏/评论赞赏.
是否可以使用禁用的构造函数和手动设置的受保护属性创建模拟对象?
这是一个愚蠢的例子:
class A {
protected $p;
public function __construct(){
$this->p = 1;
}
public function blah(){
if ($this->p == 2)
throw Exception();
}
}
class ATest extend bla_TestCase {
/**
@expectedException Exception
*/
public function testBlahShouldThrowExceptionBy2PValue(){
$mockA = $this->getMockBuilder('A')
->disableOriginalConstructor()
->getMock();
$mockA->p=2; //this won't work because p is protected, how to inject the p value?
$mockA->blah();
}
}
Run Code Online (Sandbox Code Playgroud)
所以我想注入受保护的p值,所以我不能.我应该定义setter或IoC,还是我可以用phpunit来做这个?
我想知道如何用Zend_Test编写PHPUnit测试,一般用PHP编写.
我TestMyClass.php在同一个文件中有两个类定义(单元测试类),并且PHP Code Sniffer抱怨每个类必须自己在一个文件中.我怎么能压制这个警告?
class MyClassImpl implements MyInterface
{
// ...
}
class MyClassTest extends \PHPUnit_Framework_TestCase
{
// ...
}
Run Code Online (Sandbox Code Playgroud) 我想在我的项目中测试特定的testClass,因为有很多测试类失败了,我只想一次测试一个类.
我在以下文件夹中创建了测试类\test\repositories\ApplicationVersionFormat.php :
<?php
use App\Repositories\ApplicationVersionFormat;
class ApplicationVersionFormatTest extends \TestCase
{
public function testValidFormat()
{
$version = '1.2.3';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(true,$appVersion->isValidVersion($version));
}
public function testInvalidFormat()
{
$version = '11.2.3';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
public function testInvalidFormat2()
{
$version = '1.22.3';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
public function testInvalidFormat3()
{
$version = '1.2.33';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
public function testInvalidFormat4()
{
$version = '11.22.33';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试了这个以下命令,但这些都不起作用:
phpunit …您如何进行单元测试curl实现?
public function get() {
$ch = curl_init($this->request->getUrl());
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if (!strstr($type, 'application/json')) {
throw new HttpResponseException('JSON response not found');
}
return new HttpResponse($code, $result);
}
Run Code Online (Sandbox Code Playgroud)
我需要测试返回的内容类型,以便它可以抛出异常.
php ×10
phpunit ×10
unit-testing ×3
curl ×1
laravel ×1
laravel-5.2 ×1
pear ×1
phpcs ×1
testing ×1