duc*_*unt 3 php phpunit zend-framework
我有以下类“MyControllerTest”用于测试“MyController”。我想在此类的不同测试中共享同一个对象,即“$algorithms”,但在尝试在不同位置添加变量后,我不知道该怎么做:
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() { };
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能共享这个对象?任何帮助,将不胜感激!
你有几个选择
直接在 setUp 方法中声明数据装置并声明一个私有变量来保存它。所以你可以在其他测试方法中使用它。
在你的测试类中声明一个私有函数来保存你的数据夹具。如果您需要数据夹具,只需调用私有方法
使用保存数据夹具的方法创建一个实用程序类。Utility 类在 setUp 函数中初始化。在 MyControllerTest 中声明一个私有变量,用于保存 Utility 类的实例。当测试需要夹具时,只需从 Utility 实例调用夹具方法。
示例 1
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);
}
}
Run Code Online (Sandbox Code Playgroud)
示例 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(....);
}
}
Run Code Online (Sandbox Code Playgroud)
示例 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());
}
}
Run Code Online (Sandbox Code Playgroud)
但要注意在某些测试中共享和更改的夹具。这真的会弄乱你的测试套件。
| 归档时间: |
|
| 查看次数: |
1343 次 |
| 最近记录: |