Introduction to Testing
Learn what testing is, why we need it, and how to do it.
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. ...