Learn TDD/Unit Testing with Laravel from Real-life Projects
3 min read
I know for many developers out there, everybody always wants to learn how to write unit testing and work with TDD (basically write test, code, write test, code,....).
But the thing is, every time you Google "how to write unit test", you'll get a bunch of articles. But most of them are just telling you the "definition" with some BS examples, that's all, like:
You need to write this test, that test,...
Write a func
a + b = cthen write the test for the func
IKR? As a newbie (in terms of unit testing), how I suppose to write a freaking test with that?
We're living in a world where we write code that interacts with many things eg: databases, services,... =>
a+b=c is not gonna help you anything.
To learn more about the unit test (especially from scratch), the best way should be: Learning from a Real-life Project.
So, I did prepare some Real-life Projects using PHP + Laravel to help you guys learn unit testing.
Test definition in my projects
- Where we're going to test the methods of the Class. The test case would involve DB.
- Same as the Unit, but this test won't involve a database, all we need to do is mock and then test (faster result for simple test cases)
- HTTP Test to test the endpoints, checking the results,...
- HTTP Test to check the whole flow of your big feature (call API A, then B, C,...)
What did you cover?
I'm covering these things under my test cases.
Mostly all methods that the application is going to use
Reliable data assertions
Proof? Check my CodeCov status 😆
Where should I start?
First, let's have an overall look at the project
Second, let's read the test cases, you will know what you need to test and how to write tests
(Optional) What you will learn as well
Set up a simple CI (Continuous Integration). Basically, set up the projects, run some commands to validate the app then run the test.
Code coverage setup & integration with CodeCov
All available in the
.github folder 😉
KivaNote is a simple application which provides APIs for the Simple Note app.
It also has a 3rd party - ImgBB. So you'll learn how to test the 3rd party services too.
ShipSaaS Ready provides a bunch of basic entities, cool helpers and libraries for your next ideas (SaaS projects or anything).
EloquentDocs helps you to generate the phpDoc for your Eloquent models, these will be added:
columns (with type)
Thanks, hope you guys enjoy all projects and give them a ⭐ on GitHub if it helps.
Also, let's always try to write unit testing for any of your projects. Thinking like: "I need to write some tests after coding this". It would help a lot.