1

我在functions.php(phpbb)中有我的自定义函数testfunction_phpbb(),我想测试它。

function testfunction_phpbb()
{
global $user
...
...
...
//if valid user
return 1;
//else
return 0
}

当我执行以下测试用例时,我发现 $user 总是为空(我没有得到全局上下文)。问题是当我在 phpbb、drupal、joomla 等中测试函数时。通过 phpunit+selenium 进行测试时如何获取上下文?

<?php
require_once './includes/functions.php';
class globaltest extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://localhost/");
}
public function testMyTestCase()
{
$this->open("/");
$this->click("link=phpBB3");
$this->waitForPageToLoad("30000");
$this->click("link=Login");
$this->waitForPageToLoad("30000");
$this->type("id=username", "admin");
$this->type("id=password", "admin123");
$this->click("name=login");
$this->waitForPageToLoad("30000");
$returnvalue = testfunction_phpbb();
PHPUnit_Framework_Assert::assertEquals('1',$returnvalue);
}
}
?>
4

1 回答 1

0

正如您所发现的,全局变量会破坏单元测试。因此,它们不是一个好主意,普遍接受的解决方案是:

我通常会选择依赖注入方法,因为你会更容易使用它。但是,如果您使用的是框架,则可能会被全局变量卡住,因此第二个选项是您最好的选择。

于 2012-07-27T09:48:26.360 回答