Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run Laravel Database Seeder from PHPUnit Test setUp?

Tags:

I am trying to recreate the database before each test in some PHPUnit test cases. I am using Laravel 5.3. Here is TestCase:

class CourseTypesTest extends TestCase {     public function setUp()     {         parent::setUp();         Artisan::call('migrate');         Artisan::call('db:seed', ['--class' => 'TestDatabaseSeeder ', '--database' => 'testing']);     }      /**      * A basic functional test example.      *      * @return void      */     public function test_list_course_types()     {         $httpRequest = $this->json('GET', '/api/course-types');         $httpRequest->assertResponseOk();         $httpRequest->seeJson();      }      public function tearDown()     {         Artisan::call('migrate:reset');         parent::tearDown();     } } 

Running phpunit fails with error:

$ phpunit PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 2.19 seconds, Memory: 12.00MB

There was 1 error:

1) CourseTypesTest::test_list_course_types ReflectionException: Class TestDatabaseSeeder does not exist

D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:749 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:644 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:709 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:74 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:63 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:2292 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:64 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:508 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:169 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Command\Command.php:254 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:155 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:821 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:187 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:118 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Application.php:107 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:218 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:237 D:\www\learn-laravel\my-folder-api\tests\rest\CourseTypesTest.php:17

ERRORS! Tests: 1, Assertions: 0, Errors: 1.

but this class exists:
TestDatabaseSeeder inside database/seeds

like image 939
gandra404 Avatar asked Jan 22 '17 12:01

gandra404


People also ask

Which command to run seeding in Laravel?

Laravel Seeding Creating a Seeder To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory. Generated seeders will contain one method: run . You may insert data into your database in this method.

How do you seed a single seeder in Laravel?

Writing SeedersA seeder class only contains one method by default: run . This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.


1 Answers

Since version 5.8 you can do:

// Run the DatabaseSeeder... $this->seed();  // Run a single seeder... $this->seed(OrderStatusesTableSeeder::class); 

Take a look at the documentation

like image 124
Santi Barbat Avatar answered Sep 30 '22 11:09

Santi Barbat