lsst.utils  14.0-9-g11010eb+2
Classes | Functions | Variables
lsst.utils.tests Namespace Reference

Classes

class  ExecutablesTestCase
 Test that executables can be run and return good status. More...
 
class  MemoryTestCase
 Check for memory leaks since memId0 was allocated. More...
 
class  TestCase
 Subclass of unittest.TestCase that adds some custom assertions for convenience. More...
 

Functions

def init ()
 
def run (suite, exit=True)
 Exit with the status code resulting from running the provided test suite. More...
 
def sort_tests (tests)
 Go through the supplied sequence of test suites and sort them to ensure that MemoryTestCases are at the end of the test list. More...
 
def suiteClassWrapper (tests)
 
def findFileFromRoot (ifile)
 Find file which is specified as a path relative to the toplevel directory; we start in $cwd and walk up until we find the file (or throw IOError if it doesn't exist) More...
 
def getTempFilePath (ext, expectOutput=True)
 Return a path suitable for a temporary file and try to delete the file on success. More...
 
def inTestCase (func)
 A decorator to add a free function to our custom TestCase class, while also making it available as a free function. More...
 
def assertRaisesLsstCpp (testcase, excClass, callableObj, args, kwargs)
 
def debugger (exceptions)
 Decorator to enter the debugger when there's an uncaught exception. More...
 
def plotImageDiff (lhs, rhs, bad=None, diff=None, plotFileName=None)
 Plot the comparison of two 2-d NumPy arrays. More...
 
def assertFloatsAlmostEqual (testCase, lhs, rhs, rtol=sys.float_info.epsilon, atol=sys.float_info.epsilon, relTo=None, printFailures=True, plotOnFailure=False, plotFileName=None, invert=False, msg=None)
 Highly-configurable floating point comparisons for scalars and arrays. More...
 
def assertFloatsNotEqual (testCase, lhs, rhs, kwds)
 
def assertFloatsEqual (testCase, lhs, rhs, kwargs)
 
def assertClose (args, kwargs)
 
def assertNotClose (args, kwargs)
 

Variables

 psutil = None
 
 dafBase = None
 
int memId0 = 0
 
int nleakPrintMax = 20
 
 open_files = set()
 

Function Documentation

◆ assertClose()

def lsst.utils.tests.assertClose (   args,
  kwargs 
)

Definition at line 672 of file tests.py.

◆ assertFloatsAlmostEqual()

def lsst.utils.tests.assertFloatsAlmostEqual (   testCase,
  lhs,
  rhs,
  rtol = sys.float_info.epsilon,
  atol = sys.float_info.epsilon,
  relTo = None,
  printFailures = True,
  plotOnFailure = False,
  plotFileName = None,
  invert = False,
  msg = None 
)

Highly-configurable floating point comparisons for scalars and arrays.

The test assertion will fail if all elements lhs and rhs are not equal to within the tolerances specified by rtol and atol. More precisely, the comparison is:

abs(lhs - rhs) <= relTo*rtol OR abs(lhs - rhs) <= atol

If rtol or atol is None, that term in the comparison is not performed at all.

When not specified, relTo is the elementwise maximum of the absolute values of lhs and rhs. If set manually, it should usually be set to either lhs or rhs, or a scalar value typical of what is expected.

Parameters
[in]testCaseunittest.TestCase instance the test is part of
[in]lhsLHS value(s) to compare; may be a scalar or array-like of any dimension
[in]rhsRHS value(s) to compare; may be a scalar or array-like of any dimension
[in]rtolRelative tolerance for comparison; defaults to double-precision epsilon.
[in]atolAbsolute tolerance for comparison; defaults to double-precision epsilon.
[in]relToValue to which comparison with rtol is relative.
[in]printFailuresUpon failure, print all inequal elements as part of the message.
[in]plotOnFailureUpon failure, plot the originals and their residual with matplotlib. Only 2-d arrays are supported.
[in]plotFileNameFilename to save the plot to. If None, the plot will be displayed in a a window.
[in]invertIf True, invert the comparison and fail only if any elements are equal. Used to implement assertFloatsNotEqual, which should generally be used instead for clarity.
[in]msgString to append to the error message when assert fails.

Definition at line 550 of file tests.py.

◆ assertFloatsEqual()

def lsst.utils.tests.assertFloatsEqual (   testCase,
  lhs,
  rhs,
  kwargs 
)
Assert that lhs == rhs (both numeric types, whether scalar or array).

See assertClose (called with rtol=atol=0) for more information.

Definition at line 662 of file tests.py.

◆ assertFloatsNotEqual()

def lsst.utils.tests.assertFloatsNotEqual (   testCase,
  lhs,
  rhs,
  kwds 
)
Fail a test if the given floating point values are equal to within the given tolerances.

See assertClose for more information.

Definition at line 652 of file tests.py.

◆ assertNotClose()

def lsst.utils.tests.assertNotClose (   args,
  kwargs 
)

Definition at line 679 of file tests.py.

◆ assertRaisesLsstCpp()

def lsst.utils.tests.assertRaisesLsstCpp (   testcase,
  excClass,
  callableObj,
  args,
  kwargs 
)

Definition at line 457 of file tests.py.

◆ debugger()

def lsst.utils.tests.debugger (   exceptions)

Decorator to enter the debugger when there's an uncaught exception.

To use, just slap a "@debugger()" on your function.

You may provide specific exception classes to catch as arguments to the decorator function, e.g., "@debugger(RuntimeError, NotImplementedError)". This defaults to just 'AssertionError', for use on unittest.TestCase methods.

Code provided by "Rosh Oxymoron" on StackOverflow: http://stackoverflow.com/questions/4398967/python-unit-testing-automatically-running-the-debugger-when-a-test-fails

Definition at line 463 of file tests.py.

◆ findFileFromRoot()

def lsst.utils.tests.findFileFromRoot (   ifile)

Find file which is specified as a path relative to the toplevel directory; we start in $cwd and walk up until we find the file (or throw IOError if it doesn't exist)

This is useful for running tests that may be run from dir/tests or dir

Definition at line 331 of file tests.py.

◆ getTempFilePath()

def lsst.utils.tests.getTempFilePath (   ext,
  expectOutput = True 
)

Return a path suitable for a temporary file and try to delete the file on success.

If the with block completes successfully then the file is deleted, if possible; failure results in a printed warning. If a file is remains when it should not, a RuntimeError exception is raised. This exception is also raised if a file is not present on context manager exit when one is expected to exist. If the block exits with an exception the file if left on disk so it can be examined. The file name has a random component such that nested context managers can be used with the same file suffix.

Parameters
[in]extfile name extension, e.g. ".fits"
[in]expectOutputIf true, a file should be created within the context manager. If false, a file should not be present when the context manager is exited.
Returns
path for a temporary file. The path is a combination of the caller's file path and the name of the top-level function, as per this simple example:
# file tests/testFoo.py
import unittest
class FooTestCase(unittest.TestCase):
def testBasics(self):
self.runTest()
def runTest(self):
with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
# if tests/.tests exists then tmpFile = "tests/.tests/testFoo_testBasics.fits"
# otherwise tmpFile = "testFoo_testBasics.fits"
...
# at the end of this "with" block the path tmpFile will be deleted, but only if
# the file exists and the "with" block terminated normally (rather than with an exception)
...

Definition at line 358 of file tests.py.

◆ init()

def lsst.utils.tests.init ( )
Initialize the memory tester

Definition at line 69 of file tests.py.

◆ inTestCase()

def lsst.utils.tests.inTestCase (   func)

A decorator to add a free function to our custom TestCase class, while also making it available as a free function.

Definition at line 448 of file tests.py.

◆ plotImageDiff()

def lsst.utils.tests.plotImageDiff (   lhs,
  rhs,
  bad = None,
  diff = None,
  plotFileName = None 
)

Plot the comparison of two 2-d NumPy arrays.

NOTE: this method uses matplotlib and imports it internally; it should be wrapped in a try/except block within packages that do not depend on matplotlib (including utils).

Parameters
[in]lhsLHS values to compare; a 2-d NumPy array
[in]rhsRHS values to compare; a 2-d NumPy array
[in]badA 2-d boolean NumPy array of values to emphasize in the plots
[in]diffdifference array; a 2-d NumPy array, or None to show lhs-rhs
[in]plotFileNameFilename to save the plot to. If None, the plot will be displayed in a a window.

Definition at line 491 of file tests.py.

◆ run()

def lsst.utils.tests.run (   suite,
  exit = True 
)

Exit with the status code resulting from running the provided test suite.

Definition at line 79 of file tests.py.

◆ sort_tests()

def lsst.utils.tests.sort_tests (   tests)

Go through the supplied sequence of test suites and sort them to ensure that MemoryTestCases are at the end of the test list.

Returns a combined TestSuite.

Definition at line 93 of file tests.py.

◆ suiteClassWrapper()

def lsst.utils.tests.suiteClassWrapper (   tests)

Definition at line 123 of file tests.py.

Variable Documentation

◆ dafBase

lsst.utils.tests.dafBase = None

Definition at line 50 of file tests.py.

◆ memId0

int lsst.utils.tests.memId0 = 0

Definition at line 55 of file tests.py.

◆ nleakPrintMax

int lsst.utils.tests.nleakPrintMax = 20

Definition at line 56 of file tests.py.

◆ open_files

lsst.utils.tests.open_files = set()

Definition at line 59 of file tests.py.

◆ psutil

lsst.utils.tests.psutil = None

Definition at line 45 of file tests.py.