Chapter 5. Testing your software
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 help you make sure new features are built correctly, and you can run these tests again 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. Such tests are rigorous and often mathematically provable. That’s pretty cool, but it 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 you’ll get a chance to write some tests yourself.