dec | |
unittest | |
TestCase | Test Case Class Stub with extra testing functions. |
TimeExpired | |
run(name, file, locals) | Run all the tests in the module |
istest(func) | Decorator to mark a function or method as a test |
eq_(a, b[, msg]) | Shorthand for ‘assert a == b, “%r != %r” % (a, b) |
set_trace() | Call pdb.set_trace in the calling frame, first restoring sys.stdout to the real output stream. |
raises(*exceptions) | Test must raise one of expected exceptions to pass. |
make_decorator(func) | Wraps a test decorator so as to properly replicate metadata |
nottest(func) | Decorator to mark a function or method as not a test |
with_setup([setup, teardown]) | Decorator to add setup and/or teardown methods to a test function:: @with_setup(setup, teardown) def test_something(): ” ... |
ok_(expr[, msg]) | Shorthand for assert. |
timed(limit) | Test must finish within specified time limit to pass. |
slow(t) | Labels a test as ‘slow’. |
setastest([tf]) | Signals to nose that this function is or is not a test |
skipif(skip_condition[, msg]) | Make function raise SkipTest exception if skip_condition is true |
skipknownfailure(f) | Decorator to raise SkipTest for test known to fail |
with_args(*v, **kw) | Decorator to define a functions argspec: .. |
Inheritance diagram for mmf.utils.mmf_test:
Test tools:
These test tools are based on the nose testing interface, and using some of the conventions from SciPy.
http://projects.scipy.org/numpy/wiki/TestingGuidelines
Note
The following functionality is obsolete. Use nosetests instead.
This module facilitates this by providing an initialization of these functions: Use the following template at the end of your module definition files:
# Testing
from mmf.utils.mmf_test import run
run(__name__, __file__, locals())
This will modify the local dictionary defining test() and test_suites() as well as providing runtime code to run the tests if the module is executed.
These default functions allow for two types of tests:
Doctest code in the docstrings of the class. See the doctest module for details about how these work.
Define a subclasses of the TestCase class provided here. This may be defined in either the module file, or (preferably) in a separate testing module.
The name of the testing module is the same as the module but with test_ prepended. This may be in either the same directory as the module, or in a subdirectory called tests. (These two paths are automatically searched. Of course, the test-module may also occur anywhere on sys.path).
In addition to these two function, this module also provides a class called Test that provides better control over testing (for example, this will provide code coverage analysis.)
Bases: object
Test Case Class Stub with extra testing functions.
Methods
assertAlmostEqual(exact, approx[, abs_tol, ...]) | Fail if the two objects are unequal as determined by their difference compared with the specified absolute and relative tolerances. |
ok_(res[, msg]) |
x.__init__(...) initializes x; see help(type(x)) for signature
Fail if the two objects are unequal as determined by their difference compared with the specified absolute and relative tolerances.
Parameters : | exact, approx : float or array
abs_err, rel_tol : float or (float, float)
msg : str
|
---|
Examples
>>> tc = TestCase()
There is no problem if one of the tolerances (the absolute tolerance here) is met:
>>> tc.assertAlmostEqual(0.5, 0.5 + _abs_tol/1.01)
but there is a failure if neither tolerance is met:
>>> tc.assertAlmostEqual(0.5, 0.5 + _abs_tol*1.01)
Traceback (most recent call last):
...
AssertionError: Numbers not almost equal.
Max abs_err: 1.43219e-14 > 1.42109e-14
Max rel_err: 2.86438e-14 > 1.42109e-14
Again, no problem if at least one of the tolerances (the relative tolerance here) is met:
>>> tc.assertAlmostEqual(2, 2*(1.0 + _abs_tol/1.01))
but there is a again failure if neither tolerance is met:
>>> tc.assertAlmostEqual(2, 2*(1.0 + _abs_tol*1.01))
Traceback (most recent call last):
...
AssertionError: Numbers not almost equal.
Max abs_err: 2.88658e-14 > 1.42109e-14
Max rel_err: 1.44329e-14 > 1.42109e-14
Here is an example of providing two different errors. If the tolerances are met, then there is no problem as before:
>>> tc.assertAlmostEqual(2, 2*(1.0 + _abs_tol/1.01),
... abs_tol=(_abs_tol, _abs_tol*2),
... rel_tol=(_rel_tol, _rel_tol*2))
The looser tolerances suppress errors, but the tighter tolerances will generate a warning. (First we turn warnings into errors so we can see the behaviour.)
>>> warnings.simplefilter('error', UserWarning)
>>> tc.assertAlmostEqual(2, 2*(1.0 + _abs_tol*1.01),
... abs_tol=(_abs_tol, _abs_tol*2),
... rel_tol=(_rel_tol, _rel_tol*2))
Traceback (most recent call last):
...
UserWarning: Numbers not almost equal.
Max abs_err: 2.88658e-14 > 1.42109e-14
Max rel_err: 1.44329e-14 > 1.42109e-14
NaN‘s trigger a different type of error message than unmet tolerances:
>>> tc.assertAlmostEqual(np.nan, 0)
Traceback (most recent call last):
...
AssertionError: NaN encountered.
These tests also work with arrays and lists, using numpy to provide proper vectorization:
>>> tc.assertAlmostEqual([0, 1, 2], [0, 1, 2])
>>> tc.assertAlmostEqual([np.nan, 1, 2], [0, np.nan, 2])
Traceback (most recent call last):
...
AssertionError: 2 of 2*3 components are NaN.
>>> tc.assertAlmostEqual([0.1, 1, 2], [0, 1, 2])
Traceback (most recent call last):
...
AssertionError: 2 of 3 components of arrays not almost equal.
Max abs_err: 0.1 > 1.42109e-14
Max rel_err: 1 > 1.42109e-14
>>> tc.assertAlmostEqual([0.1, 1, 2], [0, 1, 2],
... abs_tol=(_abs_tol, 0.2))
Traceback (most recent call last):
...
UserWarning: 2 of 3 components of arrays not almost equal.
Max abs_err: 0.1 > 1.42109e-14
Max rel_err: 1 > 1.42109e-14
>>> tc.assertAlmostEqual([np.nan, 1, 2], [0, np.nan, 2])
Traceback (most recent call last):
...
AssertionError: 2 of 2*3 components are NaN.
Bases: exceptions.AssertionError
x.__init__(...) initializes x; see help(type(x)) for signature
Run all the tests in the module
This is to be used by including the following code in your source:
# Testing
from mmf.utils.mmf_test import run
run(__name__, __file__, locals())
Note
This functionality is obsolete. Use nosetests instead.
Parameters : | name : The name of the current module. Typically this is set to
file : The name of the current file. Typically this is set to
locals : Dictionary of local variables for the module.
|
---|
Decorator to mark a function or method as a test
Shorthand for ‘assert a == b, “%r != %r” % (a, b)
Call pdb.set_trace in the calling frame, first restoring sys.stdout to the real output stream. Note that sys.stdout is NOT reset to whatever it was before the call once pdb is done!
Test must raise one of expected exceptions to pass.
Example use:
@raises(TypeError, ValueError)
def test_raises_type_error():
raise TypeError("This test passes")
@raises(Exception)
def test_that_fails_by_passing():
pass
If you want to test many assertions about exceptions in a single test, you may want to use assert_raises instead.
Wraps a test decorator so as to properly replicate metadata of the decorated function, including nose’s additional stuff (namely, setup and teardown).
Decorator to mark a function or method as not a test
Decorator to add setup and/or teardown methods to a test function:
@with_setup(setup, teardown)
def test_something():
" ... "
Note that with_setup is useful only for test functions, not for test methods or inside of TestCase subclasses.
Shorthand for assert. Saves 3 whole characters!
Test must finish within specified time limit to pass.
Example use:
@timed(.1)
def test_that_fails():
time.sleep(.2)
Labels a test as ‘slow’.
The exact definition of a slow test is obviously both subjective and hardware-dependent, but in general any individual test that requires more than a second or two should be labeled as slow (the whole suite consits of thousands of tests, so even a second is significant).
Signals to nose that this function is or is not a test
Parameters : | tf : bool
|
---|
Examples
@setastest(False) def func_with_test_in_name(arg1, arg2): pass
...
This decorator cannot use the nose namespace, because it can be called from a non-test module. See also istest and nottest in nose.tools
Make function raise SkipTest exception if skip_condition is true
Parameters : | skip_condition : bool
|
---|---|
Returns : | decorator : function
|
Notes
You will see from the code that we had to further decorate the decorator with the nose.tools.make_decorator function in order to transmit function name, and various other metadata.
Decorator to define a functions argspec:
Examples
>>> def A(x,y): print "A(%r,%r)"%(x,y)
>>> def B(y,z): print "A(%r,%r)"%(y,z)
>>> @with_args(A,B)
... def C(t,args):
... print "C(%r)"%(t)
... with_args(A)(args)
... with_args(B)(args)
>>> C(x=1,y=2,z=3,t=4)
C(4)
A(1,2)
B(2,3)