Tests
We recomment using PEST testing suite instead of phpunit to write tests for yout applications. We have seen that PEST works very well to organise your tests and dataset etc in a better way.
You can run the test in a couple different ways ./vendor/bin/pest & php artisan test built into Laravel. Our test are run on each pull request to a package.
Feature
Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint. Generally, most of your tests should be feature tests. These types of tests provide the most confidence that your system as a whole is functioning as intended.
Unit
Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests probably focus on a single method. Tests within your "Unit" test directory do not boot your Laravel application and therefore are unable to access your application's database or other framework services.
Benefits
There are some great benefits to writing tests:
- Fewer bugs.
- Happier customers.
- Happier employers.
- Confident developers. You won’t fear breaking something when coming back to the project after a while.
- New hires can be productive from day one, especially if you follow Laravel’s guidelines. Changed some code? No problem. Just run ‘php artisan test’ see what you broke, fix, and repeat!
Being able to make a project immensely more stable thanks to automated testing will do wonders for your career.
How To
Copy the .env file to .env.testing file and this environment file will be used when you run your tests. Remember to add values to the .env.testing file for your local setup.
Some guidelines when setting up testing for a project
- Lazily refresh your database before each test
- Make use of factories to help you with fake data and tests
- Test against the production stack whenever it’s possible
- Use database transactions to rollback changes after each test
- Don’t waste API calls, use mocks
- Prevent stray HTTP requests to identify slow tests
Tutorial:
Laracasts has a great FREE testing course on PEST that you can upskill.
