将Laravel phpunit测试绑定到路由

时间:2015-10-11 09:11:37

标签: php unit-testing laravel testing phpunit

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

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

这可能吗?

提前致谢。

1 个答案:

答案 0 :(得分:3)

您可以创建如下路线:

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?