Join Justin Yost for an in-depth discussion in this video Why use unit testing?, part of Test-Driven Development in PHP with PHPUnit.
- So why should you care about unit testing? Unit testing adds a few things to your code that you didn't have before. First, it ensures that it works correctly now and in the future. Second, unit testing adds additional documentation regarding what your code is doing and how to use it. Third, unit testing reduces the chance of bugs or issues in your code. Fourth, unit testing improves your ability to re-factor your already existing code.
Finally, unit testing helps you write better design code. We'll explore each point in more detail and hopefully throughout this course, you'll come to see how each is demonstrated through the use of unit testing more broadly and TDD more specifically. Unit testing ensures your code works correctly today and in the future. One aspect of unit testing is writing test suites that can test the full application using these unit tests. They're easy to run and can be run multiple times when developing a new feature to ensure that as you add or change parts of the application everything else still works.
Since we add a test before adding new code, we ensure the code works correctly because it must pass the test. For example, if we have a library of currency formatters and add a new method to display values correctly for India, we write a test to ensure our code produces the output we expect. At the same time, we still have tests to ensure our currency methods for the United States, the United Kingdom, China, Japan, Germany, Russia, France, and so on are also all still working even as we add this new feature.
Unit testing provides you an additional documentation point to what your code does and how it operates. You quite literally write code that says, call this method with this input and it should return this output. Testing in another way for you to share information to other developers. Here are the typical and unusual scenarios for this particular method. Unit testing, quite obviously reduces the chance of bugs or issues in your code. Now to be clear, no programming practice will ever fully eliminate every bug or issue from your software.
However, we can point to actual research showing that TDD produces less defects in software. The widely cited 2008 paper realizing quality improvement through Test Driven Development compared four programming teams split between Microsoft and IBM. The teams that used TDD had a 40 to 90% decrease in defects at only an additional cost of 15 to 35% in initial development time.
All teams agreed the cost was well worth it compared to the reduced amount of maintenance needed. And it's not hard to see why. Spend a little more money and time up front with TDD and you will produce a product that has less bugs and thus requires less time in maintenance down the road. A different paper that looked at a variety of agile software development practices, Agile in Embedded Software Development, found a variety of measures that point to TDD being valuable and reducing bugs.
This paper demonstrated that agile software development using TDD increased the quality of the software produced. It made coding an easier process. It also produced more focused planning and design in the software development. Test Driven Development also enabled a timely feedback cycle between your test and your code. The paper also demonstrated that you would have less bugs when you used Test Driven Development. Because you have tests that prove what your code does today as you re-factor your application, the tests provide a level of assurance that says, yes, given this input, I still produce the same output.
Provide me a massive application any day with tests to re-factor versus a tiny application without any tests. That tiny application, even though it may not do much, I have no idea how to ensure that as I re-factor that application, it will still do what it's supposed to be doing. Tests are that assurance. Because of the way tests operate, writing testable code tends to result in better-designed software, in short, the patterns for developing well-designed applications.
Small isolated methods, dependency injection, and so on are all patterns that if followed, makes it easier for you to write tests for your code. Following Test Driven Development and writing the tests first improves this process. You will find yourself writing better software because of the choices unit testing will lead you towards.
- Why use unit testing?
- Writing unit tests
- Extending unit tests
- Filtering PHPUnit tests
- Building dummy objects
- Working with data providers
- Writing an exception-based test
- Using TDD tactics
- Using PHPUnit advanced tactics, such as database tests