Previous topic

mmf.utils.mmf_sympy

This Page

mmf.utils.mmf_test

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:

Inheritance diagram of 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

  • test(): Run the tests for the module.
  • bench(): Run benchmarks for the module.

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:

  1. Doctest code in the docstrings of the class. See the doctest module for details about how these work.

  2. 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.)

class mmf.utils.mmf_test.TestCase[source]

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])
__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

assertAlmostEqual(exact, approx, abs_tol=1.4210854715202004e-14, rel_tol=1.4210854715202004e-14, msg=None, relax=None)[source]

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

Exact reference value and test value.

abs_err, rel_tol : float or (float, float)

Success if abs_err < abs_tol or rel_err < rel_tol where abs_err = abs(exact - approx) and rel_err = abs_err/abs(exact). If pairs are provided, then the test fails only if the maximum tolerances are not met, but a warning is generated if the minimum tolerances are not met.

msg : str

Optional message.

Examples

Here is a simple example of usage:
>>> 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.
ok_(res, msg=None)[source]
class mmf.utils.mmf_test.TimeExpired

Bases: exceptions.AssertionError

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

mmf.utils.mmf_test.run(name, file, locals)[source]

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

the global variable __name__.

file : The name of the current file. Typically this is set to

the global variable __file__.

locals : Dictionary of local variables for the module.

Typically this is set to the result of locals().

mmf.utils.mmf_test.istest(func)

Decorator to mark a function or method as a test

mmf.utils.mmf_test.eq_(a, b, msg=None)

Shorthand for ‘assert a == b, “%r != %r” % (a, b)

mmf.utils.mmf_test.set_trace()

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!

mmf.utils.mmf_test.raises(*exceptions)

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.

mmf.utils.mmf_test.make_decorator(func)

Wraps a test decorator so as to properly replicate metadata of the decorated function, including nose’s additional stuff (namely, setup and teardown).

mmf.utils.mmf_test.nottest(func)

Decorator to mark a function or method as not a test

mmf.utils.mmf_test.with_setup(setup=None, teardown=None)

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.

mmf.utils.mmf_test.ok_(expr, msg=None)

Shorthand for assert. Saves 3 whole characters!

mmf.utils.mmf_test.timed(limit)

Test must finish within specified time limit to pass.

Example use:

@timed(.1)
def test_that_fails():
    time.sleep(.2)
mmf.utils.mmf_test.slow(t)

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).

mmf.utils.mmf_test.setastest(tf=True)

Signals to nose that this function is or is not a test

Parameters :

tf : bool

If True specifies this is a test, not a test otherwise

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

mmf.utils.mmf_test.skipif(skip_condition, msg=None)

Make function raise SkipTest exception if skip_condition is true

Parameters :

skip_condition : bool

Flag to determine whether to skip test (True) or not (False)

msg : string

Message to give on raising a SkipTest exception

Returns :

decorator : function

Decorator, which, when applied to a function, causes SkipTest to be raised when the skip_condition was True, and the function to be called normally otherwise.

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.

mmf.utils.mmf_test.skipknownfailure(f)[source]

Decorator to raise SkipTest for test known to fail

mmf.utils.mmf_test.with_args(*v, **kw)

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)