【问题标题】:Is it possible to unit test, php files other than class file using PHPUnit?是否可以使用 PHPUnit 对类文件以外的 php 文件进行单元测试?
【发布时间】:2016-03-15 15:58:43
【问题描述】:

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

【问题讨论】:

    标签: php unit-testing phpunit


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-09-08
      • 2017-09-27
      • 1970-01-01
      • 2012-08-31
      • 2011-01-15
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2012-03-26
      相关资源
      最近更新 更多