testing Package

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.

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

nose_tools Module

Non-standard functions for the ‘nose’ testing framework.

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

Decorator to indicate a test is deprecated.

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

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

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

Decorator to indicate a test should be skipped.

trait_assert_tools Module

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

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)[source]

Assert that the class trait changes exactly n times.

Used in a with statement to assert that a class trait has changed during the execution of the code inside the with context block (similar to the assertRaises method).

Please note that the context manager returns itself and the user can introspect the information of the fired events by accessing the events attribute of the context object. The attribute is a list with tuple elements containing the arguments of an on_trait_change event signature (<object>, <name>, <old>, <new>).

Example:

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

my_class = MyClass()

with self.assertTraitChanges(my_class, 'number') as ctx:
   my_class.number = 3.0

self.assert(ctx.events, [(my_class, 'number', 2.0, 3.0)]
Parameters:
  • obj (HasTraits) – The HasTraits class instance who’s class trait will change.
  • xname (str) – The extended trait name of trait changes to listen too.
  • count (int, 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.

Note

  • Checking if the provided xname corresponds to valid traits in the class is not implemented yet.
assertTraitDoesNotChange(obj, xname)[source]

Assert that no trait event is fired.

Used in a with statement to assert that a class trait has not changed during the execution of the code inside the with statement block.

Example:

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

my_class = MyClass()

with self.assertTraitDoesNotChange(my_class, 'name'):
     my_class.number = 2.0
Parameters:
  • obj (HasTraits) – The HasTraits class instance who’s class trait will change.
  • xname (str) – The extended trait name of trait changes to listen too.

Note

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

Table Of Contents

Previous topic

protocols Package

Next topic

util Package

This Page