test module¶
This module contains functions to aid in the unit testing of modules in the package (py.test-based)
Functions¶
-
putil.test.
assert_arg_invalid
(fpointer, pname, *args, **kwargs)¶ Asserts whether a function raises a
RuntimeError
exception with the message'Argument `*pname* ` is not valid'
, where*pname*
is the value of the pname argument, when called with given positional and/or keyword argumentsParameters: - fpointer (callable) – Object to evaluate
- pname (string) – Parameter name
- args (tuple) – Positional arguments to pass to object
- kwargs (dictionary) – Keyword arguments to pass to object
Raises: - AssertionError (Did not raise)
- RuntimeError (Illegal number of arguments)
-
putil.test.
assert_exception
(fpointer, extype, exmsg, *args, **kwargs)¶ Asserts an exception type and message within the Py.test environment. If the actual exception message and the expected exception message do not literally match then the expected exception message is treated as a regular expression and a match is sought with the actual exception message
Parameters: - fpointer (callable) – Object to evaluate
- extype (type) – Expected exception type
- exmsg (string) – Expected exception message (can have regular expressions)
- args (tuple) – Positional arguments to pass to object
- kwargs (dictionary) – Keyword arguments to pass to object
For example:
>>> import putil.test, putil.eng >>> try: ... putil.test.assert_exception( ... putil.eng.peng, ... RuntimeError, ... 'Argument `number` is not valid', ... {'number':5, 'frac_length':3, 'rjust':True} ... ) ... except AssertionError: ... raise RuntimeError('Test failed') Traceback (most recent call last): ... RuntimeError: Test failed
Raises: - AssertionError (Did not raise)
- RuntimeError (Illegal number of arguments)
-
putil.test.
assert_prop
(cobj, prop_name, value, extype, exmsg)¶ Asserts whether a class property raises a given exception when assigned a given value
Parameters: - cobj (class object) – Class object
- prop_name (string) – Property name
- extype (Exception type object, i.e. RuntimeError, TypeError, etc.) – Exception type
- exmsg (string) – Exception message
-
putil.test.
assert_ro_prop
(cobj, prop_name)¶ Asserts that a class property cannot be deleted
Parameters: - cobj (class object) – Class object
- prop_name (string) – Property name
-
putil.test.
compare_strings
(actual, ref, diff_mode=False)¶ Compare two strings. Lines are numbered, differing characters are colored yellow and extra characters (characters present in one string but not in the other) are colored red
Parameters: - actual (string) – Text produced by software under test
- ref (string) – Reference text
- diff_mode (boolean) – Flag that indicates whether the line(s) of the actual and reference strings are printed one right after the other (True) of if the actual and reference strings are printed separately (False)
Raises: - AssertionError(Strings do not match)
- RuntimeError(Argument `actual` is not valid)
- RuntimeError(Argument `diff_mode` is not valid)
- RuntimeError(Argument `ref` is not valid)
-
putil.test.
exception_type_str
(exobj)¶ Returns an exception type string
Parameters: exobj (type (Python 2) or class (Python 3)) – Exception Return type: string For example:
>>> import putil.test >>> exception_type_str(RuntimeError) 'RuntimeError'
-
putil.test.
get_exmsg
(exobj)¶ Returns exception message (Python interpreter version independent)
Parameters: exobj (exception object) – Exception object Return type: string