功能单元测试

发布于 2024-12-20 20:23:16 字数 145 浏览 3 评论 0原文

我有一个 PHP 文件,其中有一些函数(不包含在类中)。我正在使用 PHPUnit 进行测试。当我尝试以简单的方式从包含函数的文件生成测试文件时,日志显示:

Could not find class...

是否有可能测试不是方法的函数?

I have got a PHP file in which there are some functions (not included in a class). I am using PHPUnit to testing. When I try to generate in a simply way a test file from a file containing functions, the log says:

Could not find class...

Is there any possibility to test functions which are not methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

老旧海报 2024-12-27 20:23:16

是的,您可以使用以下内容:

includes/functions.phptests

function my_function() {
    return true;
}

/MyFunctionTest.php

require_once '../includes/functions.php';

class MyFunctionTest extends PHPUnit_Framework_TestCase
{
    public function testReturnValue()
    {
        $return_value = my_function();
        $this->assertTrue($return_value);
    }
}

因此,只要您的函数在范围内,您就可以从测试方法中调用它,就像任何其他 PHP 框架或项目中的任何其他 PHP 函数一样。

Yes, you can with something like this:

includes/functions.php

function my_function() {
    return true;
}

tests/MyFunctionTest.php

require_once '../includes/functions.php';

class MyFunctionTest extends PHPUnit_Framework_TestCase
{
    public function testReturnValue()
    {
        $return_value = my_function();
        $this->assertTrue($return_value);
    }
}

So as long as your function is within scope you can call it from your test method just like any other PHP function in any other PHP framework or project.

时光礼记 2024-12-27 20:23:16

如果我是对的,那么 PhpUnit 仅适用于类,因此适用于方法。因此只需将它们转换为用于测试目的的方法即可。应该不难。

If I'm right then PhpUnit works only with classes hence methods. So just convert them into the methods for testing purpose. Shouldn't be hard.

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