Skip to main content
`Ctrl`+`K`

# `silx.test`:

## `silx.test`

This package provides test of the root modules

run_tests(module='silx', verbosity=0, args=())

  Run tests in a subprocess

  Parameters:

    - module (`str` | `None`) – Name of the silx module to test (default: ‘silx’)

    - verbosity (`int`) – Requested level of verbosity

    - args (`Sequence`[`str`]) – List of extra arguments to pass to pytest

## `silx.test.utils`

Utilities for writing tests.

- `temp_dir()` provides a with context to create/delete a temporary directory.

utilstest = <silx.utils.ExternalResources.ExternalResources object>

  This is the instance to be used. Singleton-like feature provided by module

temp_dir()

  with context providing a temporary directory.

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

add_gaussian_noise(y, stdev=1.0, mean=0.0)

  Add random gaussian noise to synthetic data.

  Parameters:

    - y (ndarray) – Array of synthetic data

    - mean (float) – Mean of the gaussian distribution of noise.

    - stdev (float) – Standard deviation of the gaussian distribution of noise.

  Returns:

    Array of data with noise added

add_poisson_noise(y)

  Add random noise from a poisson distribution to synthetic data.

  Parameters:

    y (ndarray) – Array of synthetic data

  Returns:

    Array of data with noise added

add_relative_noise(y, max_noise=5.0)

  Add relative random noise to synthetic data. The maximum noise level is given in percents.

  An array of noise in the interval [-max_noise, max_noise] (continuous uniform distribution) is generated, and applied to the data the following way:

  \(yn = y * (1. + noise / 100.)\)

  Parameters:

    - y (ndarray) – Array of synthetic data

    - max_noise (float) – Maximum percentage of noise

  Returns:

    Array of data with noise added

## `silx.gui.utils.testutils`

Helper class to write Qt widget unittests.

qWaitForWindowExposedAndActivate(window, timeout=None)

  Waits until the window is shown in the screen.

  It also activates the window and raises it.

  See QTest.qWaitForWindowExposed for details.

class TestCaseQt(methodName='runTest')

  Base class to write test for Qt stuff.

  It creates a QApplication before running the tests. WARNING: The QApplication is shared by all tests, which might have side effects.

  After each test, this class is checking for widgets remaining alive. To allow some widgets to remain alive at the end of a test, set the allowedLeakingWidgets attribute to the number of widgets that can remain alive at the end of the test. With PySide, this test is not run for now as it seems PySide is leaking widgets internally.

  All keyboard and mouse event simulation methods call qWait(20) after simulating the event (as QTest does on Mac OSX). This was introduced to fix issues with continuous integration tests running with Xvfb on Linux.

  DEFAULT_TIMEOUT_WAIT = 100

    Default timeout for qWait

  TIMEOUT_WAIT = 0

    Extra timeout in millisecond to add to qSleep, qWait and qWaitForWindowExposed.

    Intended purpose is for debugging, to add extra time to waits in order to allow to view the tested widgets.

  classmethod setUpClass()

    Makes sure Qt is inited

  classmethod tearDownClass()

    Hook method for deconstructing the class fixture after running all tests in the class.

  setUp()

    Get the list of existing widgets.

  tearDown()

    Hook method for deconstructing the test fixture after testing it.

  property qapp

    The QApplication currently running.

  Press = 0

    Key press action code

  Release = 1

    Key release action code

  Click = 2

    Key click action code

  property QTest

    The Qt QTest class from the used Qt binding.

  keyClick(widget, key, modifier=0, delay=-1)

    Simulate clicking a key.

    See QTest.keyClick for details.

  keyClicks(widget, sequence, modifier=0, delay=-1)

    Simulate clicking a sequence of keys.

    See QTest.keyClick for details.

  keyEvent(action, widget, key, modifier=0, delay=-1)

    Sends a Qt key event.

    See QTest.keyEvent for details.

  keyPress(widget, key, modifier=0, delay=-1)

    Sends a Qt key press event.

    See QTest.keyPress for details.

  keyRelease(widget, key, modifier=0, delay=-1)

    Sends a Qt key release event.

    See QTest.keyRelease for details.

  mouseClick(widget, button, modifier=None, pos=None, delay=-1)

    Simulate clicking a mouse button.

    See QTest.mouseClick for details.

  mouseDClick(widget, button, modifier=None, pos=None, delay=-1)

    Simulate double clicking a mouse button.

    See QTest.mouseDClick for details.

  mouseMove(widget, pos=None, delay=-1)

    Simulate moving the mouse.

    See QTest.mouseMove for details.

  mousePress(widget, button, modifier=None, pos=None, delay=-1)

    Simulate pressing a mouse button.

    See QTest.mousePress for details.

  mouseRelease(widget, button, modifier=None, pos=None, delay=-1)

    Simulate releasing a mouse button.

    See QTest.mouseRelease for details.

  qSleep(ms)

    Sleep for ms milliseconds, blocking the execution of the test.

    See QTest.qSleep for details.

  classmethod qWait(ms=None)

    Waits for ms milliseconds, events will be processed.

    See QTest.qWait for details.

  qWaitForWindowExposed(window, timeout=None)

    Waits until the window is shown in the screen.

    See QTest.qWaitForWindowExposed for details.

  exposeAndClose(widget)

    Wait for expose a widget, flag it delete on close, and close it.

  classmethod qWaitForDestroy(ref)

    Wait for Qt object destruction.

    Use a weakref as parameter to avoid any strong references to the object.

    It have to be used as following. Removing the reference to the object before calling the function looks to be expected, else `deleteLater()` will not work.

        ref = weakref.ref(self.obj)
        self.obj = None
        self.qWaitForDestroy(ref)

    Parameters:

      ref (weakref) – A weakref to an object to avoid any reference

    Returns:

      True if the object was destroyed

    Return type:

      bool

  logScreenShot(level=40)

    Take a screenshot and log it into the logging system if the logger is enabled for the expected level.

    The screenshot is stored in the directory “./build/test-debug”, and the logging system only log the path to this file.

    Parameters:

      level – Logging level

class SignalListener

  Util to listen a Qt event and store parameters

  clear()

    Clear stored data

  callCount()

    Returns how many times the listener was called.

    Return type:

      int

  arguments(callIndex=None, argumentIndex=None)

    Returns positional arguments optionally filtered by call count id or argument index.

    Parameters:

      - callIndex (int) – Index of the called data

      - argumentIndex (int) – Index of the positional argument.

  karguments(callIndex=None, argumentName=None)

    Returns positional arguments optionally filtered by call count id or name of the keyword argument.

    Parameters:

      - callIndex (int) – Index of the called data

      - argumentName (int) – Name of the keyword argument.

  partial(*args, **kargs)

    Returns a new partial object which when called will behave like this listener called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords.

getQToolButtonFromAction(action)

  Return a QToolButton corresponding to a QAction.

  Parameters:

    action (QAction) – The QAction from which to get QToolButton.

  Returns:

    A QToolButton associated to action or None.