Chapter 13. Testing: Ensuring Your Program Does the Right Thing
How do you know your program does what you think it does? Even with your careful attention to detail, are you sure that sales tax calculation function works properly? How do you know?
This chapter is about giving you the peace of mind that comes with answers to those questions. Unit testing is a way of making assertions about small bits of your codeââIf I put these values into this function, I should get this other value out.â By creating tests that check the behavior of your code in appropriate situations, you can have confidence in how your program behaves.
PHPUnit is the de facto standard for writing tests for PHP code. Your tests are themselves little bits of PHP code. The following section describes how to install PHPUnit. âWriting a Testâ shows some code and a first test for it. Use and run this code to make sure youâve got PHPUnit installed properly and understand the basic pieces of a test.
Then, âIsolating What You Testâ looks at how to narrow the focus of what youâre testing for maximum efficiency.
âTest-Driven Developmentâ extends the tested code by adding some tests for code that doesnât exist yet, and then adding the code to make the tests pass. This technique can be a handy way to ensure youâre writing code that is tested properly.
At the end of this chapter, âMore Information About Testingâ provides details on where to find more information about PHPUnit and testing in ...
Get Learning PHP now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.