taurus taurus

Previous topic

taurus.qt.uic

Next topic

ModuleExplorer

This Page

taurus.test

Taurus provides a framework for testing. This framework intends to facilitate evaluation, bug finding and integration of contributed code/patches, as well as to promote test driven development in Taurus.

The first implementation of this Framework is an outcome of the [Sardana Enhancement Proposal 5 (SEP5)](http://sourceforge.net/p/sardana/wiki/SEP5/)

Ideally, bug reports should be accompanied by a test revealing the bug, whenever possible.

The first tests implemented are focused on Unit Tests, but the same framework should be used for integration and system tests as well.

The taurus.test.testsuite module provides an autodiscovered suite for all tests implemented in Taurus.

The following are some key points to keep in mind when using this framework:

  • The Taurus test framework is based on unittest which should be imported from taurus.external in order to be compatible with all versions of python supported by Taurus.
  • all test-related code is contained in submodules named test which appear in any module of taurus.
  • test-related code falls in one of these three categories:
    • actual test code (classes that derive from unittest.TestCase)
    • utility classes/functions (code to simplify development of test code)
    • resources (accessory files required by some test). They are located in subdirectories named res

For a more complete description of the conventions on how to write tests with the Taurus testing framework, please refer to the [SEP5](http://sourceforge.net/p/sardana/wiki/SEP5/).

Classes

Functions

calculateTestFuzziness(test, maxtries=100, maxfails=10, **kwargs)

Estimate the fuzziness of a test by running it many times and counting the failures. In this context, we assume that there is an underlying problem and but that the test is not perfect and only fails (triggers the problem) with a certain failure rate.

Parameters:
  • testname (:class:~`str`) – test name. see: unittest.TestLoader.loadTestsFromName()
  • maxtries (:class:~`int`) – maximum number of runs
  • maxfails (:class:~`int`) – maximum number of failed runs
Return type:

:class:~`tuple`

Returns:

a tuple (f,df,n) where f is the failure rate, df is its standard deviation, and n is the number of consecutive times that the test should be passed to have a confidence>99%% that the bug is fixed’

getResourcePath(resmodule, fname='')

Returns the absolute path to the directory in which the resource module named resmodule is implemented. If filename is passed, the path to the filename in such directory is returned, e.g.:

getResourcePath(‘foo.test.res’, ‘bar.txt’) –>
absolute path to <taurus>/foo/test/res/bar.txt

It raises ImportError if resmodule does not exist and RuntimeError if fname does not exist)

Parameters:
  • resmodule (:class:~`str`) – name of a resource module
  • fname (:class:~`str`) – the name of a resource file present in the resmodule directory
Return type:

:class:~`str`

Returns:

absolute path to the resource file (or to the resource directory if fname is not passed)

insertTest(klass=None, helper_name=None, test_method_name=None, test_method_doc=None, tested_name=None, **helper_kwargs)

Decorator that inserts test methods from a helper method that accepts arguments. insertTest provides a very economic API for creating new tests for a given class based on a helper method. insertTest accepts the following arguments:

  • helper_name (str): the name of the helper method. insertTest will

    insert a test method which calls the helper with any the helper_kwargs (see below).

  • test_method_name (str): Optional. Name of the test method to be used.

    If None given, one will be generated from the tested class and helper names.

  • test_method_doc (str): Optional. The docstring for the inserted test

    method (this shows in the unit test output). If None given, a default one is generated which includes the input parameters and the helper name.

  • tested_name (str): Optional. The name of the class or feature being

    tested (if given, it will be used in default method names and docstrings).

  • **helper_kwargs: All remaining keyword arguments are passed to the

    helper.

This decorator can be considered a “base” decorator. It is often used to create other decorators in which the helper method is pre-set, as in the following example:

isPos = functools.partial(insertTest, helper_name='isPositive')

@isPos(x=2)
@isPos(x=10)
class Foo(unittest.TestCase):
    def isPositive(self, x):
        self.assertTrue(x > 0)
loopSubprocess(target, maxtries=100, maxfails=10, okvalues=(0, ), args=(), kwargs=None)

Run a callable as a subprocess maxtries times or until it fails maxfails times and report the number of tries and failures. The callable is run as a subprocess and it is considered to run fine if the subprocess exit code is in the okValues list.

Parameters:
  • target (:class:~`callable`) – a callable test
  • maxtries (:class:~`int`) – maximum number of runs
  • maxfails (:class:~`int`) – maximum number of failed runs
  • okvalues (:class:~`seq`) – a sequence containing exit values of cmd which are considered to be successful runs.
  • args (:class:~`seq`) – arguments for running the target function
  • kwargs (:class:~`dict`) – keyword arguments for running the target function
Return type:

:class:~`tuple`

Returns:

a tuple of ints: tries, failures

loopTest(testname, maxtries=100, maxfails=10)

Run a test maxtries times or until it fails maxfails times and report the number of tries and failures.

Parameters:
  • testname (:class:~`str`) – test name. see: unittest.TestLoader.loadTestsFromName()
  • maxtries (:class:~`int`) – maximum number of runs
  • maxfails (:class:~`int`) – maximum number of failed runs
Return type:

:class:~`tuple`

Returns:

a tuple of ints: tries, failures

skipUnlessGui()

Decorator to indicate that the given test should be skipped if GUI Tests are not enabled.

It can be applied both to unittest.TestCase classes and to test methods:

class FooTest(unittest.TestCase):
    def test_something_which_does_not_need_gui()
        (...)

    @skipUnlessGui()
    def test_something_that requires_gui()
        (...)

@skipUnlessGui()
class GUITest(unittest.TestCase):
    (...)

Note: using skipUnlessGui is equivalent to:

@skipunless(taurus.test.GUI_TESTS_ENABLED, ‘requires GUI’)