如何在PHPUnit中共享不同测试中的对象
我有以下类“MyControllerTest”,用于测试“MyController”。 我想在此类的不同测试中共享同一个对象,即“$algorithms”,但在尝试在不同位置添加变量后,我不知道如何做到这一点:如何在PHPUnit中共享不同测试中的对象,php,zend-framework,phpunit,Php,Zend Framework,Phpunit,我有以下类“MyControllerTest”,用于测试“MyController”。 我想在此类的不同测试中共享同一个对象,即“$algorithms”,但在尝试在不同位置添加变量后,我不知道如何做到这一点: class MyControllerTest extends Zend_Test_PHPUnit_ControllerTestCase { // $algorithms = ... <----- does not work public static funct
class MyControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
// $algorithms = ... <----- does not work
public static function main()
{
$suite = new PHPUnit_Framework_TestSuite("MyControllerTest");
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
public function setUp()
{
$this->bootstrap = new Zend_Application(
'testing',
APPLICATION_PATH . '/configs/application.ini'
);
$configuration = Zend_Registry::get('configuration');
// $algorithms = ... <----- does not work
parent::setUp();
}
public function tearDown()
{
}
public function test1() {
// this array shared in different tests
$algorithms[0] = array(
"class" => "Mobile",
"singleton" => "true",
"params" => array(
"mobile" => "true",
"phone" => "false"
)
);
...
}
public function test2() { };
}
类MyControllerTest扩展Zend_Test_PHPUnit_ControllerTestCase
{
//$algorithms=…bootstrap=新的Zend_应用程序(
"测试",,
应用程序路径。“/configs/APPLICATION.ini”
);
$configuration=Zend_Registry::get('configuration');
//$algorithms=…“移动”,
“singleton”=>“true”,
“params”=>数组(
“mobile”=>“true”,
“电话”=>“错误”
)
);
...
}
公共函数test2(){};
}
如何共享此对象?
任何帮助都将不胜感激 这里有两种选择
class MyControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
private $alg;
public function setUp()
{
# variable that holds the data fixture
$this->alg = array(....);
}
public function test1()
{
$this->assertCount(1, $this->alg);
}
}
例2
class MyControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function test1()
{
$this->assertCount(1, $this->getAlgo());
}
# Function that holds the data fixture
private function getAlgo()
{
return array(....);
}
}
例3
class Utility
{
# Function that holds the data fixture
public function getAlgo()
{
return array(....);
}
}
class MyControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
private $utility;
public function setUp()
{
$this->utility = new Utility();
}
public function test1()
{
$this->assertCount(1, $this->utility->getAlgo());
}
}
但是要小心在某些测试中共享和更改的装置。这真的会弄乱你的测试套件。在你的setUp()中定义它,然后类中的所有测试都可以使用它(尽管你现有的$algorithms定义是一个数组,而不是一个对象)。。。。但是请记住,
$algorithms
!==<代码>$this->algorithms谢谢!这是我的第一节物理测试课,我有点困惑。我更喜欢第一种选择!:-D