silx.testutils: General purpose test utilities

Utilities for writting tests.

  • ParametricTestCase provides a TestCase.subTest() replacement for Python < 3.4
  • TestLogging with context or the test_logging() decorator enables testing the number of logging messages of different levels.
  • temp_dir() provides a with context to create/delete a temporary directory.
class silx.testutils.ParametricTestCase(methodName='runTest')[source]

TestCase with subTest support for Python < 3.4.

Add subTest method to support parametric tests. API is the same, but behavior differs: If a subTest fails, the following ones are not run.

subTest(*args, **kwds)[source]

Use as unittest.TestCase.subTest method in Python >= 3.4.

class silx.testutils.TestLogging(logger=None, critical=None, error=None, warning=None, info=None, debug=None, notset=None)[source]

Context checking the number of logging messages from a specified Logger.

It disables propagation of logging message while running.

This is meant to be used as a with statement, for example:

>>> with TestLogging(logger, error=2, warning=0):
>>>     pass  # Run tests here expecting 2 ERROR and no WARNING from logger
...
Parameters:
  • logger (str or logging.Logger) – Name or instance of the logger to test. (Default: root logger)
  • critical (int) – Expected number of CRITICAL messages. Default: Do not check.
  • error (int) – Expected number of ERROR messages. Default: Do not check.
  • warning (int) – Expected number of WARNING messages. Default: Do not check.
  • info (int) – Expected number of INFO messages. Default: Do not check.
  • debug (int) – Expected number of DEBUG messages. Default: Do not check.
  • notset (int) – Expected number of NOTSET messages. Default: Do not check.
Raises RuntimeError:
 

If the message counts are the expected ones.

emit(record)[source]

Override logging.Handler.emit()

silx.testutils.test_logging(logger=None, critical=None, error=None, warning=None, info=None, debug=None, notset=None)[source]

Decorator checking number of logging messages.

Propagation of logging messages is disabled by this decorator.

In case the expected number of logging messages is not found, it raises a RuntimeError.

>>> class Test(unittest.TestCase):
...     @test_logging('module_logger_name', error=2, warning=0)
...     def test(self):
...         pass  # Test expecting 2 ERROR and 0 WARNING messages
Parameters:
  • logger (str or logging.Logger) – Name or instance of the logger to test. (Default: root logger)
  • critical (int) – Expected number of CRITICAL messages. Default: Do not check.
  • error (int) – Expected number of ERROR messages. Default: Do not check.
  • warning (int) – Expected number of WARNING messages. Default: Do not check.
  • info (int) – Expected number of INFO messages. Default: Do not check.
  • debug (int) – Expected number of DEBUG messages. Default: Do not check.
  • notset (int) – Expected number of NOTSET messages. Default: Do not check.
silx.testutils.temp_dir(*args, **kwds)[source]

with context providing a temporary directory.

>>> import os.path
>>> with temp_dir() as tmp:
...     print(os.path.isdir(tmp))  # Use tmp directory

Previous topic

silx.resources: Project resources access

Next topic

Change Log

This Page