Introduction to Testing
Explore the fundamentals of automated testing in PHP web development. Understand unit, integration, and functional tests, how to implement them, and their role in maintaining application quality and detecting bugs before deployment. Discover test coverage metrics and the basics of test-driven development to improve your coding workflow.
We'll cover the following...
How do we know our freshly written code works? We can run it to see if it produces the expected result. We can run it multiple times with different inputs to see how it behaves in other cases. It makes sense to do a lot of testing while creating the code, but we won’t test every use case for every piece of code every day that we modify the application. And if the old code breaks, this might go unnoticed during manual testing.
That’s why we need automated testing. Instead of checking out code manually every time it changes, we make scripts that check it automatically. This way, we can write tests once and use them as long as they remain relevant.
How to test
Typical tests look like this:
- Prepare initial state and input data.
- Call the function we’re testing with input data.
- Check the output data or result state, and throw an error if it doesn’t look as expected.
To see how it works, let’s create a buggy function that counts words in a string and test it. ...