NIPY logo

Site Navigation

NIPY Community

Github repo

Table Of Contents

Previous topic

testing.decorators

This Page

testing.doctester

Module: testing.doctester

Inheritance diagram for nipy.testing.doctester:

Custom doctester based on Numpy doctester

To run doctests via nose, you’ll need nosetests nipy/testing/doctester.py --doctest-test, because this file will be identified as containing tests.

Classes

DocTestSkip

class nipy.testing.doctester.DocTestSkip(obj)

Bases: object

Object wrapper for doctests to be skipped.

__init__(obj)

NipyDocTestFinder

class nipy.testing.doctester.NipyDocTestFinder(verbose=False, parser=<doctest.DocTestParser instance at 0x20efcf8>, recurse=True, exclude_empty=True)

Bases: nipy.fixes.numpy.testing.noseclasses.NumpyDocTestFinder

Methods

find(obj[, name, module, globs, extraglobs]) Return a list of the DocTests that are defined by the given
__init__(verbose=False, parser=<doctest.DocTestParser instance at 0x20efcf8>, recurse=True, exclude_empty=True)

Create a new doctest finder.

The optional argument parser specifies a class or function that should be used to create new DocTest objects (or objects that implement the same interface as DocTest). The signature for this factory function should match the signature of the DocTest constructor.

If the optional argument recurse is false, then find will only examine the given object, and not any contained objects.

If the optional argument exclude_empty is false, then find will include tests for objects with empty docstrings.

find(obj, name=None, module=None, globs=None, extraglobs=None)

Return a list of the DocTests that are defined by the given object’s docstring, or by any of its contained objects’ docstrings.

The optional parameter module is the module that contains the given object. If the module is not specified or is None, then the test finder will attempt to automatically determine the correct module. The object’s module is used:

  • As a default namespace, if globs is not specified.
  • To prevent the DocTestFinder from extracting DocTests from objects that are imported from other modules.
  • To find the name of the file containing the object.
  • To help find the line number of the object within its file.

Contained objects whose module does not match module are ignored.

If module is False, no attempt to find the module will be made. This is obscure, of use mostly in tests: if module is False, or is None but cannot be found automatically, then all objects are considered to belong to the (non-existent) module, so all contained objects will (recursively) be searched for doctests.

The globals for each DocTest is formed by combining globs and extraglobs (bindings in extraglobs override bindings in globs). A new copy of the globals dictionary is created for each DocTest. If globs is not specified, then it defaults to the module’s __dict__, if specified, or {} otherwise. If extraglobs is not specified, then it defaults to {}.

NipyDoctest

class nipy.testing.doctester.NipyDoctest

Bases: nipy.fixes.numpy.testing.noseclasses.NumpyDoctest

Attributes

enableOpt
extension

Methods

addOptions(parser[, env]) Add command-line options for this plugin.
add_options(parser[, env]) Non-camel-case version of func name for backwards compatibility.
afterContext()
configure(options, config)
doctest_case_class
help() Return help for this plugin.
loadTestsFromFile(filename) Load doctests from the file.
loadTestsFromModule(module)
makeTest(obj, parent) Look for doctests in the given object, which will be a
matches(name)
options(parser[, env])
out_check_class
prepareTestLoader(loader) Capture loader’s suiteClass.
set_test_context(test)
suiteClass
test_finder_class
tolist(val)
wantFile(file)
__init__()
addOptions(parser, env=None)

Add command-line options for this plugin.

The base plugin class adds –with-$name by default, used to enable the plugin.

add_options(parser, env=None)

Non-camel-case version of func name for backwards compatibility.

Warning

DEPRECATED: Do not use this method, use options instead.

afterContext()
can_configure = False
configure(options, config)
doctest_case_class

alias of NumpyDocTestCase

doctest_ignore = ['generate_numpy_api.py', 'scons_support.py', 'setupscons.py', 'setup.py']
doctest_optflags = 12
enableOpt = None
enabled = False
extension = None
help()

Return help for this plugin. This will be output as the help section of the –with-$name option that enables the plugin.

loadTestsFromFile(filename)

Load doctests from the file.

Tests are loaded only if filename’s extension matches configured doctest extension.

loadTestsFromModule(module)
makeTest(obj, parent)

Look for doctests in the given object, which will be a function, method or class.

matches(name)
name = 'nipydoctest'
options(parser, env={'LC_NUMERIC': 'ru_RU.UTF-8', 'DEB_BUILD_GNU_SYSTEM': 'linux-gnu', 'DEB_BUILD_MULTIARCH': 'x86_64-linux-gnu', 'DEB_HOST_ARCH_ENDIAN': 'little', 'DEB_BUILD_ARCH': 'amd64', 'DEB_BUILD_ARCH_OS': 'linux', 'LC_MEASUREMENT': 'ru_RU.UTF-8', 'LC_CTYPE': 'en_US.UTF-8', 'DEB_HOST_ARCH_OS': 'linux', 'DEB_HOST_ARCH': 'amd64', 'FAKEROOTKEY': '1914526122', 'MPLCONFIGDIR': '/tmp/buildd/nipy-0.3.0/build', 'USER': 'root', 'HOME': '/tmp/buildd/nipy-0.3.0/build', 'LC_PAPER': 'ru_RU.UTF-8', 'PATH': '/tmp/buildd/nipy-0.3.0/scripts:/usr/lib/ccache:/usr/sbin:/usr/bin:/sbin:/bin', 'PBCURRENTCOMMANDLINEOPERATION': 'build', 'LD_LIBRARY_PATH': '/usr/lib/x86_64-linux-gnu/libfakeroot:/usr/lib64/libfakeroot:/usr/lib32/libfakeroot', 'MAKEFLAGS': 'w', 'LC_TIME': 'en_US.UTF-8', 'LANG': 'C', 'DEB_BUILD_ARCH_BITS': '64', 'TERM': 'screen', 'SHELL': '/bin/bash', 'DEB_BUILD_ARCH_ENDIAN': 'little', 'MAKELEVEL': '2', 'SHLVL': '7', 'PBUILDER_PKGLIBDIR': '/usr/lib/pbuilder', 'DEB_HOST_GNU_TYPE': 'x86_64-linux-gnu', 'DEB_HOST_GNU_CPU': 'x86_64', 'COWDANCER_ILISTFILE': '/.ilist', 'DEBIAN_FRONTEND': 'noninteractive', 'LC_ADDRESS': 'ru_RU.UTF-8', 'DEB_HOST_MULTIARCH': 'x86_64-linux-gnu', 'PYTHONPATH': '/tmp/buildd/nipy-0.3.0/build/lib.linux-x86_64-2.7', 'SUDO_USER': 'neurodebian', 'MFLAGS': '-w', 'DEB_BUILD_ARCH_CPU': 'amd64', 'DEB_BUILD_GNU_TYPE': 'x86_64-linux-gnu', 'USERNAME': 'root', 'LC_IDENTIFICATION': 'ru_RU.UTF-8', 'PBUILDER_PKGDATADIR': '/usr/share/pbuilder', 'DH_VERBOSE': '1', 'DEB_HOST_ARCH_BITS': '64', 'SUDO_UID': '1234', 'FAKED_MODE': 'unknown-is-root', 'MAIL': '/var/mail/root', 'LC_ALL': 'C', 'PBUILDER_SYSCONFDIR': '/etc', '_': 'debian/rules', 'LOGNAME': 'pbuilder', 'LD_PRELOAD': 'libfakeroot-sysv.so', 'SUDO_COMMAND': '/usr/bin/nd_build4all nipy_0.3.0-1.dsc', 'SUDO_GID': '1234', 'LC_MESSAGES': 'en_US.UTF-8', 'LC_COLLATE': 'en_US.UTF-8', 'LC_TELEPHONE': 'ru_RU.UTF-8', 'DEB_HOST_ARCH_CPU': 'amd64', 'LC_MONETARY': 'ru_RU.UTF-8', 'PWD': '/tmp/buildd/nipy-0.3.0/doc', 'DEB_BUILD_GNU_CPU': 'x86_64', 'LC_NAME': 'ru_RU.UTF-8', 'CCACHE_DIR': '/var/cache/pbuilder/ccache', 'DEB_HOST_GNU_SYSTEM': 'linux-gnu', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:', 'OLDPWD': '/tmp/buildd/nipy-0.3.0', 'COWDANCER_REUSE': 'yes'})
out_check_class

alias of NipyOutputChecker

prepareTestLoader(loader)

Capture loader’s suiteClass.

This is used to create test suites from doctest files.

score = 1000
set_test_context(test)
suiteClass

alias of DoctestSuite

test_finder_class

alias of NipyDocTestFinder

tolist(val)
wantFile(file)

NipyOutputChecker

class nipy.testing.doctester.NipyOutputChecker

Bases: nipy.fixes.numpy.testing.noseclasses.NumpyOutputChecker

Methods

check_output(want, got, optionflags)
output_difference(example, got, optionflags) Return a string describing the differences between the expected output for a given example (example) and the actual output (got).
check_output(want, got, optionflags)
output_difference(example, got, optionflags)

Return a string describing the differences between the expected output for a given example (example) and the actual output (got). optionflags is the set of option flags used to compare want and got.

Functions

nipy.testing.doctester.ignore_dtype(in_str)

Removes dtype=[dtype] from string in_str

Parameters :

in_str : str

String maybe containing dtype specifier

Returns :

out_str : str

String from which the dtype specifier has been removed.

Examples

>>> arr = np.arange(5, dtype='i2')

Here’s the normal repr:

>>> arr
array([0, 1, 2, 3, 4], dtype=int16)

The repr with the dtype bits removed

>>> ignore_dtype(repr(arr))
'array([0, 1, 2, 3, 4])'
>>> ignore_dtype('something(again, dtype=something)')
'something(again)'

Even if there are more closed brackets after the dtype

>>> ignore_dtype('something(again, dtype=something) (1, 2)')
'something(again) (1, 2)'

We need the close brackets to match

>>> ignore_dtype('again, dtype=something')
'again, dtype=something'
nipy.testing.doctester.round_numbers(in_str, precision)

Replace fp numbers in in_str with numbers rounded to precision

Parameters :

in_str : str

string possibly containing floating point numbers

precision : int

number of decimal places to round to

Returns :

out_str : str

in_str with any floating point numbers replaced with same numbers rounded to precision decimal places.

Examples

>>> round_numbers('A=0.234, B=12.345', 2)
'A=0.23, B=12.35'

Rounds the floating point value as it finds it in the string. This is even true for numbers with exponentials. Remember that:

>>> '%.3f' % 0.3339e-10
'0.000'

This routine will recognize an exponential as something to process, but only works on the decimal part (leaving the exponential part is it is):

>>> round_numbers('(0.3339e-10, "string")', 3)
'(0.334e-10, "string")'
nipy.testing.doctester.strip_array_repr(in_str)

Removes array-specific part of repr from string in_str

This parser only works on lines that contain only an array repr (and therefore start with array, and end with a close parenthesis. To remove dtypes in array reprs that may be somewhere within the line, use the IGNORE_DTYPE doctest option.

Parameters :

in_str : str

String maybe containing a repr for an array

Returns :

out_str : str

String from which the array specific parts of the repr have been removed.

Examples

>>> arr = np.arange(5, dtype='i2')

Here’s the normal repr:

>>> arr
array([0, 1, 2, 3, 4], dtype=int16)

The repr with the ‘array’ bits removed:

>>> strip_array_repr(repr(arr))
'[0, 1, 2, 3, 4]'