如何在laravel控制器中运行phpunit测试?

吉涅什·乔萨(Jignesh Joisar)
i want to run phpunit test in controller for


adding some data  in database and  testing api of project both 

PostAddTest类

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PostAddTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $api = "/post/add";
        $request = [
            'title' => "xyz form....",
            'content' => 'post add by xyz user.'
        ];

        $response = $this->postJson($api,$request);

        info("daa : ".print_r($response->getContent(),true));
        $this->assertTrue(true);
    }
}

如果我使用phpunit运行,则说明工作成功

vendor/phpunit/bin --filter testExample

PHPUnit 6.5.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 6.84 seconds, Memory: 28.00MB

OK (1 test, 1 assertion)

我成功了,但是

如果我使用控制器运行,则会出现这样的错误

在null {“ exception”:“ [object](Symfony \ Component \ Debug \ Exception \ FatalThrowableError(code:0))上调用成员函数make():在PostProject / vendor /上在null上调用成员函数make() laravel / framework / src / Illuminate / Foundation / Testing / Concerns / MakesHttpRequests.php:335

主控制器

public function index() {
      (new PostAddTest)->testExample()  
}
埃里克·帕特里克

您应该先调用该setUp方法。像这样:

$postAddTest = new PostAddTest;
$postAddTest->setUp();
$postAddTest->testExample();


我不知道你的使用情况,但如果你真的想运行控制器的测试,替代你可以使用Symfony的过程,并做到这一点:

use Symfony\Component\Process\Process;
...
public function index() {
  $process = new Process(array('vendor/bin/phpunit', '--configuration', 'phpunit.xml'), base_path());
  $process->run();

  // (Optional) Get the phpunit output
  $process->getOutput();

}

或使用PHP exec()函数http://php.net/manual/en/function.exec.php

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章