如何在laravel控制器中运行phpunit测试?
后测试类如何在laravel控制器中运行phpunit测试?,php,laravel,unit-testing,laravel-5,phpunit,Php,Laravel,Unit Testing,Laravel 5,Phpunit,后测试类 i want to run phpunit test in controller for adding some data in database and testing api of project both 如果我使用phpunit运行,则成功运行 vendor/phpunit/bin——过滤测试示例 namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\WithF
i want to run phpunit test in controller for
adding some data in database and testing api of project both
如果我使用phpunit运行,则成功运行
vendor/phpunit/bin——过滤测试示例
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);
}
}
我成功了,但是
若我使用控制器运行,那个么我将得到如下错误
对空{“exception”:“[object]上的成员函数make()的调用
(Symfony\Component\Debug\Exception\FatalThroTableError(代码:0):
在处对null调用成员函数make()
PostProject/vendor/laravel/framework/src/illumb/Foundation/Testing/Concerns/MakesHttpRequests.php:335
主控制器
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 6.84 seconds, Memory: 28.00MB
OK (1 test, 1 assertion)
您应该首先调用
setUp
方法。如下所示:
public function index() {
(new PostAddTest)->testExample()
}
我不知道您的用例,但如果您真的想在控制器中运行测试,作为备选方案,您可以使用并执行以下操作:
$postAddTest = new PostAddTest;
$postAddTest->setUp();
$postAddTest->testExample();
或者使用PHPexec()函数您应该首先调用
setUp
方法。如下所示:
public function index() {
(new PostAddTest)->testExample()
}
我不知道您的用例,但如果您真的想在控制器中运行测试,作为备选方案,您可以使用并执行以下操作:
$postAddTest = new PostAddTest;
$postAddTest->setUp();
$postAddTest->testExample();
或者使用PHPexec()function你说的“使用控制器运行”是什么意思?你是通过浏览器手动打开@index方法吗?通过浏览器在控制器@index方法中运行特定的phpunit测试运行你说的“使用控制器运行”是什么意思“?是否通过浏览器手动打开@index方法?在browseri wan的控制器@index方法中运行特定的phpunit测试运行,以使用
Symfony进程运行特定的测试
如何执行此操作将--filter tester示例添加到新进程中的数组中:array”('vendor/bin/phpunit'、'--configuration'、'phpunit.xml'、'--filter'、'testExample')如果要执行指定目录中的所有测试(例如tests/Unit目录中的测试),请将数组更改为:array('vendor/bin/phpunit'、'--configuration'、'phpunit.xml'、'tests/Unit'))现在已成功发送请求,但未等待响应,仅获取空输出..我想使用Symfony进程运行特定测试
如何执行此操作将--filter testExample添加到新进程中的数组中:array('vendor/bin/phpunit','--configuration',phpunit.xml','--filter',testExample'))另外,如果要在指定目录中执行所有测试(例如,tests/Unit目录中的测试),请将数组更改为:array('vendor/bin/phpunit','--configuration','phpunit.xml','tests/Unit'),现在成功发送请求,但不等待响应,仅获取空输出。。