Learn TDD/Unit Testing with Laravel from Real-life Projects

Photo by Sigmund on Unsplash

Learn TDD/Unit Testing with Laravel from Real-life Projects


3 min read

Hey guys,

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 = c then write the test for the func assertEquals(2, plus(1,1))

  • ...

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

  • Unit Test

    • Where we're going to test the methods of the Class. The test case would involve DB.
  • Quick Test

    • 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)
  • Feature Test

    • HTTP Test to test the endpoints, checking the results,...
  • Integration Test

    • 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.

  • All endpoints

  • 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

    • Routes

    • Controllers

    • Requests

    • Services

  • 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 ๐Ÿ˜‰


Repository: https://github.com/sethsandaru/kiva-laravel-tdd

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

Repository: https://github.com/shipsaas/ready

ShipSaaS Ready provides a bunch of basic entities, cool helpers and libraries for your next ideas (SaaS projects or anything).


Repository: https://github.com/sethsandaru/eloquent-docs

EloquentDocs helps you to generate the phpDoc for your Eloquent models, these will be added:

  • columns (with type)

  • accessors

  • relationships

Final words

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.