API Reference#

nti.testing#

The nti.testing module exposes the most commonly used API from the submodules (for example, nti.testing.is_true is just an alias for nti.testing.matchers.is_true). The submodules may contain other functions, though, so be sure to look at their documentation.

Importing this module has side-effects when zope.testing is importable:

  • Add a zope.testing cleanup to ensure that transactions never last past the boundary of a test. If a test begins a transaction and then fails to abort or commit it, subsequent uses of the transaction package may find that they are in a bad state, unable to clean up resources. For example, the dreaded ConnectionStateError: Cannot close a connection joined to a transaction.

  • A zope.testing cleanup also ensures that the global transaction manager is in its default implicit mode, at least for the current thread.

Changed in version 3.1.0: The mock module, or its backwards compatibility backport for Python 2.7, is now available as an attribute of this module, and as the module named nti.testing.mock. Thus, for compatibility with both Python 2 and Python 3, you can write from nti.testing import mock or from nti.testing.mock import Mock, or even just from nti.testing import Mock.

Changed in version 3.1.0: Expose the most commonly used attributes of some submodules as API on this module itself.

class nti.testing.Mock[source]#

This is an alias for unittest.mock.Mock (or its backport) made available here for convenience.

nti.testing.transactionCleanUp()[source]#

Implement the transaction cleanup described in the module documentation.

nti.testing.is_true()[source]#

Matches an object with a true boolean value.

nti.testing.is_false()[source]#

Matches an object with a false boolean value.

nti.testing.provides(iface)[source]#

Matches an object that provides the given interface.

nti.testing.implements(iface)[source]#

Matches if the object implements (is a factory for) the given interface.

nti.testing.verifiably_provides(*ifaces)[source]#

Matches if the object verifiably provides the correct interface(s), as defined by zope.interface.verify.verifyObject(). This means having the right attributes and methods with the right signatures.

Note

This does not test schema compliance. For that (stricter) test, see validly_provides().

nti.testing.validly_provides(*ifaces)[source]#

Matches if the object verifiably and validly provides the given schema (interface(s)).

Verification is done with zope.interface and verifiably_provides(), while validation is done with zope.schema.getValidationErrors().

nti.testing.validated_by(field, invalid=<class 'zope.interface.exceptions.Invalid'>)[source]#

Matches if the data is validated by the given IField.

Parameters:

invalid (exception) – The types of exceptions that are considered invalid. Anything other than this is allowed to be raised.

Changed in version 2.0.1: Add invalid and change it from Exception to zope.interface.interfaces.Invalid

nti.testing.not_validated_by(field, invalid=<class 'zope.interface.exceptions.Invalid'>)[source]#

Matches if the data is NOT validated by the given IField.

Parameters:

invalid (exception) – The types of exceptions that are considered invalid. Anything other than this is allowed to be raised.

Changed in version 2.0.1: Add invalid and change it from Exception to zope.interface.interfaces.Invalid

nti.testing.aq_inContextOf(parent)[source]#

Matches if the data is in the acquisition context of the given object.

nti.testing.time_monotonically_increases(func_or_granularity)[source]#

Decorate a unittest method with this function to cause the value of time.time() (and time.gmtime()) to monotonically increase by one each time it is called. This ensures things like last modified dates always increase.

We make three guarantees about the value of time.time() returned while the decorated function is running:

  1. It is always at least the value of the real time.time();

  2. Each call returns a value greater than the previous call;

  3. Those two constraints hold across different invocations of functions decorated.

This decorator can be applied to a method in a test case:

class TestThing(unittest.TestCase)
    @time_monotonically_increases
    def test_method(self):
      t = time.time()
       ...

It can also be applied to a bare function taking any number of arguments:

@time_monotonically_increases
def utility_function(a, b, c=1):
   t = time.time()
   ...

By default, the time will be incremented in 1.0 second intervals. You can specify a particular granularity as an argument; this is useful to keep from running too far ahead of the real clock:

@time_monotonically_increases(0.1)
def smaller_increment():
    t1 = time.time()
    t2 = time.time()
    assrt t2 == t1 + 0.1

Changed in version 2.1: Add support for time.gmtime.

Changed in version 2.1: Now thread safe.

Changed in version 2.0: The decorated function returns whatever the passed function returns.

Changed in version 2.0: Properly handle more than one argument to the underlying function.

Changed in version 2.0: Ensure that the values returned are at least the real time. Previously they were integers starting at 0.

Changed in version 2.0: Ensure that the values returned are increasing even across different invocations of a decorated function or different decorated functions. Previously different functions would return the same sequence.

Changed in version 2.0: Allow specifying the granularity of clock increments. Keep at 1.0 seconds for backwards compatibility by default.

Submodules#