phpunit 运行测试两次 - 得到两个答案。为什么?

发布于 2024-09-26 16:09:28 字数 1109 浏览 0 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野稚 2024-10-03 16:09:28

PHPUnit 有一个名为“备份全局变量”的功能,如果打开,则在测试开始时会备份全局范围内的所有变量(由当前值组成快照),并且在每次测试完成后,这些值将被恢复再次恢复为原始值。您可以在这里阅读更多相关信息: http:// sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html#content

现在让我们看看您的测试套件。

  1. test01 已准备好
  2. 备份,由所有全局变量组成(此时全局范围内的 $actions 尚未设置,因为代码尚未运行)
  3. test01 运行
  4. demo.php 被包含(感谢自动加载)并且 $actions 在全局范围内设置范围
  5. 你的断言成功了,因为 $actions 设置在全局范围
  6. test01 被拆除。全局变量返回到其原始值。此时,全局范围内的 $actions 已被销毁,因为它是在测试内部设置的,因此在测试
  7. test02 运行之前它不是全局状态的一部分。 . 并失败,因为全局范围内没有 $actions 。

直接解决您的问题:在 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.

  1. test01 is prepared
  2. backup is made of all global variables (at this point $actions in global scope is not set, because the code has not ran yet)
  3. test01 runs
  4. demo.php is included (thanks to autoload) and $actions is set in global scope
  5. your assertion succeeds, because $actions is set in global scope
  6. test01 is torn down. global variables are returned to their original value. $actions in global scope is destroyed at this point, because it was set inside the test, it was not part of the global state before the start of the test
  7. test02 runs .. and fails, because there is no $actions in global scope.

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文