Chapter 2

Writing Unit Tests

AN APPLICATION IS one of the great examples of the whole being greater than the sum of its parts. As a whole, the application potentially delivers a huge amount of value. Its component parts perform specific functions, which can be useful on their own but deliver more when combined. That said, it is clear that if any of the smaller parts of the application do not function as expected, the application as a whole is flawed or fails completely. This is where unit testing comes in.

In this chapter you learn all about the fundamental testing practice of unit tests. I will cover exactly what a unit test is, how to write one and provide you examples that assist your learning. There is also a comprehensive breakdown of some of the most widely used unit test methods, which you can refer to as a guide when writing any unit tests yourself. This chapter also seeks to convey some of the standards that Python developers expect when they read over unit tests and shows you how you can put them into practice so they become second nature. Finally, this chapter rounds off with some more advanced techniques in unit testing to give you an insight into the further usages and implementations of unit tests.

What Is Unit Testing?

In unit testing, you look to cover the application’s functionality at its most basic level. Test each individual unit of code, typically a method, in isolation to see if given certain conditions it responds in the expected way (see Figure 2-1). Breaking ...

Get Testing Python: Applying Unit Testing, TDD, BDD and Acceptance Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.