traits.testing Package

Scripts and assert tools related to running unit tests.

These scripts also allow running test suites in separate processes and aggregating the results.

traits.testing.doctest_tools Module

Tools for having doctest and unittest work together more nicely.

Eclipse’s PyDev plugin will run your unittest files for you very nicely. The doctest_for_module function allows you to easily run the doctest for a module along side your standard unit tests within Eclipse.

traits.testing.doctest_tools.doctest_for_module(module)[source]

Create a TestCase from a module’s doctests that will be run by the standard unittest.main().

Example tests/test_foo.py:

import unittest

import foo
from traits.testing.api import doctest_for_module

class FooTestCase(unittest.TestCase):
    ...

class FooDocTest(doctest_for_module(foo)):
    pass

if __name__ == "__main__":
    # This will run and report both FooTestCase and the doctests in
    # module foo.
    unittest.main()

Alternatively, you can say:

FooDocTest = doctest_for_module(foo)

instead of:

class FooDocTest(doctest_for_module(foo)):
    pass

traits.testing.nose_tools Module

Non-standard functions for the ‘nose’ testing framework

This module is deprecated, and will be removed in a future release.

Deprecated since version 6.1.0.

traits.testing.nose_tools.skip(f)[source]

Stub replacement for marking a unit test to be skipped in the absence of ‘nose’.

Deprecated since version 6.1.0.

traits.testing.nose_tools.deprecated(f)[source]

Stub replacement for marking a unit test deprecated in the absence of ‘nose’.

Deprecated since version 6.1.0.

traits.testing.nose_tools.performance(f)[source]

Decorator to add an attribute to the test to mark it as a performance-measuring test.

Deprecated since version 6.1.0.

traits.testing.optional_dependencies Module

traits.testing.optional_dependencies.optional_import(name)[source]

Optionally import a module, returning None if that module is unavailable.

Parameters

name (Str) – The name of the module being imported.

Returns

None if the module is not available, and the module otherwise.

Return type

None or module

traits.testing.unittest_tools Module

Trait assert mixin class to simplify test implementation for Trait Classes.

traits.testing.unittest_tools.reverse_assertion(context, msg)[source]
class traits.testing.unittest_tools.UnittestTools[source]

Bases: object

Mixin class to augment the unittest.TestCase class with useful trait related assert methods.

assertTraitChanges(obj, trait, count=None, callableObj=None, *args, **kwargs)[source]

Assert an object trait changes a given number of times.

Assert that the class trait changes exactly count times during execution of the provided function.

This method can also be used in a with statement to assert that a class trait has changed during the execution of the code inside the with statement (similar to the assertRaises method). Please note that in that case the context manager returns itself and the user can introspect the information of:

  • The last event fired by accessing the event attribute of the returned object.

  • All the fired events by accessing the events attribute of the return object.

Note that in the case of chained properties (trait ‘foo’ depends on ‘bar’, which in turn depends on ‘baz’), the order in which the corresponding trait events appear in the events attribute is not well-defined, and may depend on dictionary ordering.

Example:

class MyClass(HasTraits):
    number = Float(2.0)

my_class = MyClass()

with self.assertTraitChanges(my_class, 'number', count=1):
    my_class.number = 3.0
Parameters
  • obj (HasTraits) – The HasTraits class instance whose class trait will change.

  • trait (str) – The extended trait name of trait changes to listen to.

  • count (int or None, optional) – The expected number of times the event should be fired. When None (default value) there is no check for the number of times the change event was fired.

  • callableObj (callable, optional) – A callable object that will trigger the expected trait change. When None (default value) a trigger is expected to be called under the context manger returned by this method.

  • *args – List of positional arguments for callableObj

  • **kwargs – Dict of keyword value pairs to be passed to the callableObj

Returns

context – If callableObj is None, an assertion context manager is returned, inside of which a trait-change trigger can be invoked. Otherwise, the context is used internally with callableObj as the trigger, in which case None is returned.

Return type

context manager or None

Notes

  • Checking if the provided trait corresponds to valid traits in the class is not implemented yet.

  • Using the functional version of the assert method requires the count argument to be given even if it is None.

assertTraitDoesNotChange(obj, trait, callableObj=None, *args, **kwargs)[source]

Assert an object trait does not change.

Assert that the class trait does not change during execution of the provided function.

Parameters
  • obj (HasTraits) – The HasTraits class instance whose class trait will change.

  • trait (str) – The extended trait name of trait changes to listen to.

  • callableObj (callable, optional) – A callable object that should not trigger a change in the passed trait. When None (default value) a trigger is expected to be called under the context manger returned by this method.

  • *args – List of positional arguments for callableObj

  • **kwargs – Dict of keyword value pairs to be passed to the callableObj

Returns

context – If callableObj is None, an assertion context manager is returned, inside of which a trait-change trigger can be invoked. Otherwise, the context is used internally with callableObj as the trigger, in which case None is returned.

Return type

context manager or None

assertMultiTraitChanges(objects, traits_modified, traits_not_modified)[source]

Assert that traits on multiple objects do or do not change.

This combines some of the functionality of assertTraitChanges and assertTraitDoesNotChange.

Parameters
  • objects (list of HasTraits) – The HasTraits class instances whose traits will change.

  • traits_modified (list of str) – The extended trait names of trait expected to change.

  • traits_not_modified (list of str) – The extended trait names of traits not expected to change.

assertTraitChangesAsync(obj, trait, count=1, timeout=5.0)[source]

Assert an object trait eventually changes.

Context manager used to assert that the given trait changes at least count times within the given timeout, as a result of execution of the body of the corresponding with block.

The trait changes are permitted to occur asynchronously.

Example usage:

with self.assertTraitChangesAsync(my_object, 'SomeEvent', count=4):
    <do stuff that should cause my_object.SomeEvent to be
    fired at least 4 times within the next 5 seconds>
Parameters
  • obj (HasTraits) – The HasTraits class instance whose class trait will change.

  • trait (str) – The extended trait name of trait changes to listen to.

  • count (int, optional) – The expected number of times the event should be fired.

  • timeout (float or None, optional) – The amount of time in seconds to wait for the specified number of changes. None can be used to indicate no timeout.

assertEventuallyTrue(obj, trait, condition, timeout=5.0)[source]

Assert that the given condition is eventually true.

Parameters
  • obj (HasTraits) – The HasTraits class instance whose traits will change.

  • trait (str) – The extended trait name of trait changes to listen to.

  • condition (callable) – A function that will be called when the specified trait changes. This should accept obj and should return a Boolean indicating whether the condition is satisfied or not.

  • timeout (float or None, optional) – The amount of time in seconds to wait for the condition to become true. None can be used to indicate no timeout.

assertDeprecated()[source]

Assert that the code inside the with block is deprecated. Intended for testing uses of traits.util.deprecated.deprecated.

assertNotDeprecated()[source]

Assert that the code inside the with block is not deprecated. Intended for testing uses of traits.util.deprecated.deprecated.