Phpunit Tips And Tricks
Real time testing
Just when you need a real time testing like npm run watch this is the stuff
https://github.com/spatie/phpunit-watcher
Install:
composer require spatie/phpunit-watcher --dev
Usage :
vendor/bin/phpunit-watcher watch
Running Artisan migrate Fresh after each test
after reading this https://medium.com/helpspace/fresh-database-once-befor-testing-starts-faa2b10dc76f , I tried to make the same steps turns out it didn't work, So I tinkered around and add it in createsApplication an now it works
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
protected static $setUpHasRunOnce = false;
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (!static::$setUpHasRunOnce) {
Artisan::call('migrate:fresh');
Artisan::call(
'db:seed',
['--class' => 'DatabaseSeeder']
);
static::$setUpHasRunOnce = true;
}
return $app;
}
}
Make the test pretty
vendor/bin/phpunit tests/Unit/ --testdox