Reputation: 4303
I'm busy learning to unit test in Laravel and I have a question. I want one test that has to do with authentication to run first before running other all other tests. Can this be achieved?
I have 4 test classes with a few methods so far.
Upvotes: 2
Views: 1969
Reputation: 16283
In PHPUnit, every single test is run with a fresh environment, all the setting up and tearing down is done for every test, so the order does not matter. If you're trying to authenticate as part of your other tests, say for example you're testing your API and you need your tests to authenticate as a user, Laravel's testing facility has methods and helpers to let you do just that.
You'll need to do all of your setup in the setUp
method of your test cases. That method is executed before each and every test is run. Similarly, the tearDown
method is run after every one of your tests have completed:
public class YourTest extends TestCase
{
protected function setUp()
{
// Do your setup here
}
protected function tearDown()
{
// Your cleanup here, if you're using Mockery, this
// is a good place to call Mockery::close();
}
public function testSomething()
{
// Your test code, you can assume that setUp has
// already run and you're ready to go.
}
}
Alternatively, you could create a method that does all of your setup and call it at the beginning of each of your tests.
Upvotes: 5