# Pitfalls, testing, profiling This week will cover ‹hypothesis›, a rather useful tool for testing Python code. Hypothesis is a «property-based» testing system: unlike traditional unit testing, we do not specify exact inputs. Instead, we provide a description of an entire class of inputs; ‹hypothesis› then randomly samples the space of all inputs in that class, invoking our test cases for each such sample. The main interface to hypothesis is the ‹hypothesis.given› decorator. It is used like this: import hypothesis # python import hypothesis.strategies as s @hypothesis.given( s.lists( s.integers() ) ) def test_sorted( x ): assert sorted( x ) == x # should fail @hypothesis.given( x = s.integers(), y = s.integers() ) def test_cancel( x, y ): assert ( x + y ) - y == x # looks okay Calling the decorated function will perform a number of randomized tests. The «strategies» dictate what values will be attempted for each argument (arguments and strategies are matched by name). Prep exercises: 1. ‹inner› – dot product on 3D vectors with integer components 2. ‹cross› – same, but cross product 3. ‹part› – partitioning lists based on a predicate 4. ‹search› – binary search, an off-by-one bonanza 5. ‹sort› – sorting lists 6. ‹heap› – tests for heap-organized arrays Regular exercises: 1. ‹xxx› 2. ‹xxx› 3. ‹xxx› 4. ‹xxx› 5. ‹xxx› 6. ‹xxx›