Exploring Common Assertion Formats
00:25 Comparison assertions are intended to test conditions that compare two or more objects using comparison operators. These assertions can also include compound expressions based on Boolean operators.
Membership assertions allow you to check if a given item is present in a specific collection, such as a list, tuple, set, or dictionary. These assertions use the membership operators,
not in, to perform the required check.
Finally, you’ll see how to check the data type of objects in the context of an assertion. Type check assertions commonly involve using the built-in
isinstance() function to make sure that a given object is an instance of a certain class or classes.
Even though these are some of the most common assertion formats that you’ll find in Python code, there are many other possibilities. For example, you can use the built-in
any() functions to write assertions that check for the truth value of items in an iterable.
02:45 Your imagination is the only limit for writing useful assertions. You can write assertions using predicate or Boolean-valued functions, regular Python objects, comparison expressions, Boolean expressions, or general Python expressions.
Become a Member to join the conversation.