that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards
TDD approach • Inventor of Extreme programming • Author of SUnit unit testing approach for SmallTalk language • Co-author of “Refactoring: Improving the Design of Existing Code”
framework, sometimes referred to as “PyUnit,” is a Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent’s Smalltalk testing framework. Each is the de facto standard unit testing framework for its respective language.
a coding session, and run it again a er. • It is a good idea to implement a hook that runs all tests before pushing code to a shared repository. • A testing unit should focus on one tiny bit of functionality and prove it correct. • Each test unit must be fully independent. Each of them must be able to run alone, and also within the test suite, regardless of the order they are called. • each test must be loaded with a fresh dataset and may have to do some cleanup a erwards. This is usually handled by setUp() and tearDown() methods.
is the smallest unit of testing. It checks for a specific response to a particular set of inputs. uni est provides a base class, TestCase, which may be used to create new test cases.
manipulate data in human readable form # functions_to_test.py def multiply(x, y): pass # test_me.py from functions_to_test import multiply from unittest import TestCase class DecoratorTestCase(TestCase): def test_multiple_simple(self): result = multiply(2,2) self.assertEqual(result, 4)
parts of your system under test with mock objects and make assertions about how they have been used. It is a part of Python Standart lib (since 3 version), for Python 2.7 you should install it with Pip
collections import functools class memoized(object): '''Decorator. Caches a function's return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated). ''' def __init__(self, func): self.func = func self.cache = {} def __call__(self, *args): if not isinstance(args, collections.Hashable) : # uncacheable. a list, for instance. # better to not cache than blow up. return self.func(*args) if args in self.cache: return self.cache[args] else: value = self.func(*args) self.cache[args] = value return value def __repr__(self): '''Return the function's docstring.''' return self.func.__doc__ def __get__(self, obj, objtype): '''Support instance methods.''' return functools.partial(self.__call__, obj)