I have a Laravel 4 test class with some tests for which I want to seed my database before running the tests. Using the setup() function to reseed for each test takes far too long. However, when I try seeding in the static setupBeforeClass() function or the constructor, I obviously can't use the $this->seed() method.
But neither can I use programmatic Artisan commands, because when I do, I get the following error: PHP Fatal error: Class 'Artisan' not found in <test class name>.
Here is the code I'd like to use to seed:
Artisan::call('migrate:refresh');
Artisan::call('db:seed', array('--class'=>'TestSeeder');
Please let me know how I can seed my database once per test class rather than per test case
This is so far the best solution I found
An "improvised" but pretty clean imho way to achieve a similar effect would be to do this in
setUp
, but have it run only once (similar to whatsetupBeforeClass
does) like this:...this is my solution and it seems simple enough and works fine, solving the performance problems of seeding and rebuilding the db structure before every test run. But remember, the "right" way to do testing, that gives you the greatest confidence your tests methods don't get subtly interdependent in bug-hiding ways, is to re-seed your db before every test method, so just put seeding code in plain
setUp
if you can afford the performance penalty (for my test cases, I couldn't afford it, but ymmv...).You can do now:
in your
setUp()
method for your test.The
seed()
method accepts as a parameter the seeder class.This trait is a wonderful way of resetting the database
I had the same problem and solved with this