phpunit 运行测试两次 - 得到两个答案。为什么?
这是我的 phpunit 测试文件
<?php // DemoTest - test to prove the point
function __autoload($className) {
// pick file up from current directory
$f = $className.'.php';
require_once $f;
}
class DemoTest extends PHPUnit_Framework_TestCase {
// call same test twice - det different results
function test01() {
$this->controller = new demo();
ob_start();
$this->controller->handleit();
$result = ob_get_clean();
$expect = 'Actions is an array';
$this->assertEquals($expect,$result);
}
function test02() {
$this->test01();
}
}
?>
这是正在测试的文件
<?php // demo.php
global $actions;
$actions=array('one','two','three');
class demo {
function handleit() {
global $actions;
if (is_null($actions)) {
print "Actions is null";
} else {
print('Actions is an array');
}
}
}
?>
结果是第二次测试失败,因为 $actions 为 null。
我的问题是 - 为什么我的两次测试没有得到相同的结果?
这是phpunit的bug还是我对php的理解?
This is my phpunit test file
<?php // DemoTest - test to prove the point
function __autoload($className) {
// pick file up from current directory
$f = $className.'.php';
require_once $f;
}
class DemoTest extends PHPUnit_Framework_TestCase {
// call same test twice - det different results
function test01() {
$this->controller = new demo();
ob_start();
$this->controller->handleit();
$result = ob_get_clean();
$expect = 'Actions is an array';
$this->assertEquals($expect,$result);
}
function test02() {
$this->test01();
}
}
?>
This is the file under test
<?php // demo.php
global $actions;
$actions=array('one','two','three');
class demo {
function handleit() {
global $actions;
if (is_null($actions)) {
print "Actions is null";
} else {
print('Actions is an array');
}
}
}
?>
The result is that the second test fails because $actions is null.
My question is - why don't I get the same results for the two tests?
Is this a bug in phpunit or it is my understanding of php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PHPUnit 有一个名为“备份全局变量”的功能,如果打开,则在测试开始时会备份全局范围内的所有变量(由当前值组成快照),并且在每次测试完成后,这些值将被恢复再次恢复为原始值。您可以在这里阅读更多相关信息: http:// sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html#content
现在让我们看看您的测试套件。
直接解决您的问题:在 DemoTest.php 的开头包含 demo.php,这样 $actions 最终会在每次测试之前和之后备份和恢复的全局范围内结束。
长期修复:尽量避免使用全局变量。这只是一个坏习惯,总有比使用“global”的全局状态更好的解决方案。
PHPUnit has a feature called "backup globals", if turned on, then at the beginning of the test all variables in global scope are backed up (a snapshot is made of current values) and after each test is completed, the values will be restored again to the original values. You can read more about that here: http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html#content
Now let's look at your test suite.
Direct fix to your problem: include demo.php at the beginning of DemoTest.php, this way $actions ends up in the global scope that is backed up and restored before and after every test.
Long term fix: try to avoid the use of globals. It's just a bad habit and there are always better solutions than global state using 'global'.