Test Driven Development and Laravel


This series will guide you through the process of using TDD (Test Driven Development) in your own projects and will consist of three articles. This first article will explain what TDD is, the positives and negatives of TDD, and finally TDD in conjunction with Laravel.

The second article will take you through the process of creating your first test which will be classed as user-interface testing. This type of test will ensure the correct things are displayed on the page and that the page redirects successfully after performing certain functionality.

Laravel TDD

What is TDD?

TDD (Test Driven Development) is the term for a methodology which requires the programmer to write tests for their code before they even begin to write any actual code. Therefore the tests you write will always fail first time round; it is then your job to write code to make the test pass. Finally refactor the code if necessary.

How does it work?

TDD is an iterative process. If you follow these steps and keep repeating then you can safely say you are using test driven development:

  1. Write down all the possible scenarios for the current piece of functionality you are about to create. This includes all possible inputs, outputs and errors.
  2. Create a test for your first scenario and run the test which will obviously fail as you have not written any code yet. You will then use this failed test as an aim to create the required code to make this current test pass.
  3. You will then need to write production code and keep checking your test until your production code has successfully made the test pass
  4. Move onto your next scenario and repeat steps 2 and 3
  5. Once you have finished the test for your final scenario, run all your tests to make sure they now all pass
  6. Clean up your code by refactoring
  7. Restart the whole process moving onto your next piece of functionality

Benefits of TDD

There are quite a number of benefits of using TDD for your projects. Some of these include the following:

  • Writing tests first gets you to really consider what it is you want from your code and also makes you think about every possible scenario that the user may choose. Therefore in theory your code should work no matter what the user throws at it.
  • As you are checking tests for small chunks of functionality at a time, when a bug is found, it is easier to find and fix as you know it must be something to do with that small chunk of code. If you didn’t take a TDD approach and didn’t test till the end of the project or the end of a large sprint for example, then it would take a lot longer to not only find the problem but possibly also to fix it
  • Allows any programmer to jump into the code and change something and then run all the tests to ensure the latest bit of code hasn’t broken any previous tests
  • Also if a problem is found further down the line, as long as all programmers have been using TDD on the project, you can simply run all the tests and find out specifically what is failing by reading the name of the test and the description of the error message that the programmer who created the test wrote

Disadvantages of TDD

The only disadvantage I could think of for TDD is that it is hard to learn and get your head around the process of testing before coding anything. This can reduce your productivity up to four months after starting to use TDD. This may not be too much of an issue in a bigger company, however smaller companies with lots of deadlines may struggle initially implementing TDD.

TDD in conjunction with Laravel

Laravel makes using TDD much easier.

A package called PHPUnit can easily be added to your project via the composer.json file which allows you to write automated tests. We will learn how to install PHPUnit in the next tutorial in this series.

Conclusion

This article has explained the basics of Test Driven-Development, its advantages, disadvantages, and how we can use PHPUnit to write automated tests in your Laravel Projects.

Next time, we will install PHPUnit in Laravel and write our very first test.