是否可以使用 PHPUnit 对类文件以外的 php 文件进行单元测试


Is it possible to unit test, php files other than class file using PHPUnit?

我是PHPUnit的新手。我有没有任何类的 php 文件。我从阅读文档中了解到,PHPUnit 将类视为一个单元。那么PHPUnit是否将类视为一个单元?是否可以测试没有任何类的 php 文件?

当然,你绝对可以测试其他PHP脚本。

class MyScriptTest extends PHPUnit_Framework_TestCase {
    public function testMyFunction() {
        include_once 'path/to/script.php';
        $result = someFunction();
        $this->assertEquals('expected result', $result);
    }
}

因此,编写 PHPUnit 测试类,并在测试中运行您希望测试的任何代码并对其做出断言。