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 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.

5.1  What is software testing?

5.1.1  Does it do what it says on the tin?

5.1.2  The anatomy of a functional test

5.2  Functional testing approaches

5.2.1  Manual testing

5.2.2  Automated testing

5.2.3  Acceptance testing

5.2.4  Unit testing

5.2.5  Integration testing

5.2.6  The testing pyramid

5.2.7  Regression testing

5.3  Statements of fact

5.4  Unit testing with unittest

5.4.1  Test organization with unittest

5.4.2  Running tests with unittest

5.4.3  Writing your first test with unittest

5.4.4  Writing your first integration test with unittest

5.4.5  Test doubles

5.4.6  Try it out