This chapter covers:
- Understanding the anatomy of a test
- Using different testing approaches for your application
- Writing tests with the
unittest
framework
- Writing tests with the
pytest
framework
- Adopting test-driven development
I’ve talked in previous chapters about writing clear code using well-named functions for maintainability, but that’s only part of the picture. As you add feature after feature, can you be sure the application still does what you meant it to? Any application you hope will live on long into the future needs some assurances of its longevity. Tests can be written as you develop new features to make sure they’re built correctly, but you can save these tests and run them each time you update your code to make sure it stays correct.
Testing can be a strict, formal process for applications that must not fail, like launching shuttles and keeping planes in flight. These tests are rigorous and often mathematically provable! That’s pretty cool, but goes way beyond what’s necessary or practical for most Python applications. In this chapter, you’ll learn about the methodology and tools Python developers use to test their code, and then you’ll get a chance to write some tests yourself.