TDD/Unit Testing with Laravel from a Real-life Project
Seth Phat
Posted on September 4, 2021
Hey guys,
I know for many developers out there, everybody always want to learn how to write unit testing, working with TDD (is basically code, write test, code, write test,....).
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 = c
then write test for the funcassertEquals(2, plus(1,1))
- ...
Right? As a newbie (in term of unit testing), how I suppose to write a freaking test with that?
We're living in the world where we write code that interacting with many things eg: Database, Services,... and a+b=c
is not gonna help you anything.
To learn more about unit test (especially from scratch), the best way should be: Learning from a Real-life Project.
So, I did prepare a simple Real-life Project using Laravel to help you guys learn unit testing.
Meet KivaNote
Repository: https://github.com/sethsandaru/kiva-laravel-tdd
A simple application which provides APIs for Simple Note app.
Test definition in KivaNote
- Unit Test
- Where we're going to test methods of the Class
- Quick Test
- Same as Unit, but this test won't involve database, all we need to do is mocking then testing.
- Feature Test
- HTTP Test to test the endpoints, checking the results,...
What did I cover?
- All endpoints
- Mostly all methods that the application is gonna use
Coverage is 87%, which is absolutely high and would give us a lot of confident.
Where should I start?
- First let's have a overall look at the project
- Routes
- Controllers
- Requests
- Services
- Second, let's read the test cases, you would know what you need to test and how to write tests
Future improvements
Yes of course, I'm going to find more external services to integrate with the project so I can write more tests (testing external services)
KivaNote has integrated with ImgBB, but seems like it's too easy haha. But it'll help.
Final words
Thanks, hope you guys enjoy it and give it a ⭐ on GitHub if it helps.
Nice day guys!!
Posted on September 4, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.