【问题标题】:Binding Laravel phpunit tests to routes将 Laravel phpunit 测试绑定到路由
【发布时间】:2015-10-12 09:34:23
【问题描述】:

我正在使用 Laravel 5.1 构建一个 RESTful API。我使用软件包附带的默认 phpunit 编写了功能测试。我可以使用phpunit 命令从命令行运行测试。

我想创建一个名为/tests 的路由,并将其绑定到 phpunit 测试。所以在我将api部署到服务器后,当我向http://api.mysite.com/test发出GET请求时,我应该会得到测试结果。

这可能吗?

提前致谢。

【问题讨论】:

  • 为什么要在生产服务器上运行测试?
  • 它只适用于这个 API,我需要向我要演示的人展示正在运行的功能测试。

标签: php unit-testing laravel testing phpunit


【解决方案1】:

你可以像这样创建一个路由:

Route::get('tests', array(function () {
 $phpunit = new PHPUnit_TextUI_TestRunner;

    try {
        $test_results = $phpunit->dorun($phpunit->getTest(__DIR__, '', 'Test.php'));
    } catch (PHPUnit_Framework_Exception $e) {
        print $e->getMessage() . "\n";
        die ("Unit tests failed.");
    }
}));

取自类似问题:Can you run PHPUnit tests from a script?

【讨论】:

    猜你喜欢
    • 2020-11-11
    • 2018-01-26
    • 2017-07-22
    • 2016-06-16
    • 2018-10-21
    • 2017-10-30
    • 2017-04-02
    • 2021-01-02
    • 2017-05-23
    相关资源
    最近更新 更多