From the course: Unit Testing and Test Driven Development in Python

Unlock this course with a free trial

Join today to access over 22,700 courses taught by industry experts.

Assert statements and exceptions

Assert statements and exceptions

- [Instructor] In this lecture, I'm going to go over using the built in Python assert statement, in your unit test, and how to test exceptions in pytests. Pytests allows the use of the built in Python assert statement for performing verifications in a unit test. The normal comparison operators can be used on all Python data types. Less than, greater than, less than or equal, greater than or equal, equal or non equal. Pytests expands on the messages that are reported for assert failures to provide more context in the test results. Validating floating point values can sometimes be difficult, as internally the value is stored as a series of binary fractions. Because of this, some comparisons that we'd expect to pass, will fail. Pytests provides the approx function, which will validate the two floating point values, or approximately the same value, as each other, to then a default tolerance of one times e to the negative six value. In some test cases, we need to verify that a function…

Contents