50: Automated Testing
This final exercise of Module 3 will teach you how to create automated tests. An automated test is code that runs other code and confirms it's still working. As a programmer your primary way to improve your work comes from automating things you do manually, and testing is one of the easiest things you can automate. Once you have a nice automated test suite you can run it every time you make a change to test that everything is still working.
What Is the Purpose of Testing?
There's a lot of people who advocate for various reasons and styles of testing, but in my experience there's really only one big benefit of automated tests:
A complete automated test suite confirms that new code doesn't break old code.
When you change code there's a probability your new code might break something else you wrote. This becomes more true as the size of your software and the team writing it increases. Once you have more than 2 people you'll run into situations where one person changes something that breaks another person's code. If you have a handful of modules connected to each other, a change in one can cause others to break.
This leads to an important consequence of having tests:
You can completely rewrite old code because your tests help ensure everything keeps working.
You do get a few additional benefits from automating your testing:
- It's automated, so you don't waste time typing the same thing over and over.
- It'll be consistent so you won't forget one of your tests.
- An automated test can help find bugs or bad designs in APIs you use, but this is less common if you're testing efficiently.
- It can give you a new perspective on your code which might help you simplify it. If your test is heinous, then maybe it's time to rewrite the code.
Register for Learn Python the Hard Way, 5th Edition (2023-2024)
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.