nti.testing.base#
Base test classes and functions for setting up ZCA.
In some cases, you may be better off using zope.component.testlayer
.
- class nti.testing.base.AbstractConfiguringObject[source]#
Bases:
object
A class for executing ZCML configuration.
Other than the attributes that are documented on this class, users are not expected to use this class or subclass it.
- configuration_context = None#
Instance attribute defined by
setUp()
that is theConfigurationMachine
that was used to load configuration data (if any). This can be used by individual methods to load more configuration data usingconfigure_packages()
or the methods fromzope.configuration
- configure_events = True#
Class attribute that is a boolean defaulting to True. When true, the
zope.component.eventtesting
module will be configured.Note
If there are any
set_up_packages
you are responsible for ensuring that thezope.component
configuration is loaded.
- features = ('devmode', 'testmode')#
Class attribute naming a sequence of strings to be added as features before loading the configuration. By default, this is
devmode
andtestmode
. (Devmode is suitable for running the application, testmode is only suitable for unit tests.)
- static get_configuration_package_for_class(klass)[source]#
Return the package that
.
means when configuring packages.For test classes that exist in a subpackage called
tests
in a module beginning withtest
, this defaults to the parent package. E.g., if klass isnti.appserver.tests.test_app.TestApp
then this isnti.appserver
.
- set_up_packages = ()#
Class attribute naming a sequence of package objects or strings naming packages. These will be configured, in order, using ZCML. The
configure.zcml
package from each package will be loaded. Instead of a package object, each item can be a tuple of (filename, package); in that case, the given file (usuallymeta.zcml
) will be loaded from the given package.
Bases:
PatchingMixin
,TestCase
Base class for testing that can share most global data (e.g., ZCML configuration) between unit tests. This is far more efficient, if the global data (e.g., ZCA component registry) is otherwise cleaned up or not mutated between tests.
Under zope.testing and nose2, this is handled by treating the class as a layer through
SharedTestBaseMetaclass
.Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Class-level attribute that determines whether we should only collect garbage when tearing down the class.
Invokes
sharedCleanup()
for every test.
Subclasses must call this method. It cleans up the global state.
It also disables garbage collection until
tearDownClass()
is called ifHANDLE_GC
is True. This way, we can collect just one generation and be sure to clean up any weak references that were created during this run. (Which is necessary, as ZCA heavily uses weak references, and when that is mixed with IComponents instances that are in a ZODB, if weak references persist and aren’t cleaned, bad things can happen. Seenti.dataserver.site
for details.) This isFalse
by default for speed; set it to true if your TestCase will be creating new (possibly synthetic) sites/site managers.
Invokes
sharedCleanup()
for every test.
Subclasses must call this method. It cleans up global state and performs garbage collection if
HANDLE_GC
is true.
- class nti.testing.base.AbstractTestBase(methodName='runTest')[source]#
Bases:
CleanUp
,PatchingMixin
,TestCase
Base class for testing. Inherits the setup and teardown functions for
zope.testing.cleanup.CleanUp
; one effect this has is to cause the component registry to be reset after every test.Note
Do not use this when you use
module_setup()
andmodule_teardown()
, as the inheritedsetUp()
will undo the effects of the module setup.Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- get_configuration_package()[source]#
See
AbstractConfiguringObject.get_configuration_package_for_class()
.
- class nti.testing.base.ConfiguringTestBase(methodName='runTest')[source]#
Bases:
AbstractConfiguringObject
,AbstractTestBase
Test case that can be subclassed when ZCML configuration is desired.
Configuration is established by the class attributes documented on
AbstractConfiguringObject
.Note
The ZCML configuration is executed for each test.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- configure_packages(set_up_packages=(), features=(), context=<object object>, configure_events=True, package=None)#
Configure additional packages. This should only be done in the
setUp
method of a subclass. Note that this is called bysetUp
.
- configure_string(zcml_string)[source]#
Execute the given ZCML string.
Tests may use this after
setUp
is called (this includes in the implementation of your ownsetUp
function).
- setUp()#
Clean up global data.
- tearDown(clear_configuration_context=True, super_tear_down=None)#
Clean up global data.
- class nti.testing.base.PatchingMixin[source]#
Bases:
object
Mixin class adding support for dynamic
unittest.mock
patches.New in version 4.0.0.
- patch(*args, **kwargs)[source]#
API for subclasses. All args are passed through to
unittest.mock.patch
which is then started and registered for cleanup.This is intended to be used in
setUp
or individual test methods when what you might need to patch is dynamic.Returns the result of
patch.start()
, i.e., a mock object.New in version 4.0.0.
Bases:
AbstractConfiguringObject
,AbstractSharedTestBase
Test case that can be subclassed when ZCML configuration is desired.
Configuration is established by the class attributes documented on
AbstractConfiguringObject
. (Theconfiguration_context
is also a class attribute.)Note
The ZCML configuration is only executed once, before any tests are run.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Configure additional packages. This should only be done in the
setUpClass
method of a subclass after calling the super class. Note that this is called bysetUpClass
.
-
New in version 2.1.0.
Subclasses must call this method. It cleans up the global state.
It also disables garbage collection until
tearDownClass()
is called ifHANDLE_GC
is True. This way, we can collect just one generation and be sure to clean up any weak references that were created during this run. (Which is necessary, as ZCA heavily uses weak references, and when that is mixed with IComponents instances that are in a ZODB, if weak references persist and aren’t cleaned, bad things can happen. Seenti.dataserver.site
for details.) This isFalse
by default for speed; set it to true if your TestCase will be creating new (possibly synthetic) sites/site managers.
Invokes
sharedCleanup()
for every test.
Subclasses must call this method. It cleans up global state and performs garbage collection if
HANDLE_GC
is true.
Bases:
type
A metaclass that converts the nose-specific use of
setUpClass
andtearDownClass
into a layer that also works with zope.testrunner (which is generally better than nose2).This works because nose2 picks one or the other, and it chooses layers over setUp/tearDownClass—only one of them is called. (If that changes, it’s easy to workaround.)
Registers a cleanup to happen for every test, regardless of whether the test is using shared configuration or not.
- nti.testing.base.module_setup(set_up_packages=(), features=('devmode', 'testmode'), configure_events=True)[source]#
A module-level fixture for configuring packages.
Either import this as
setUpModule
at the module level, or call it to perform module level set up from your own function with that name. If you use this, you must also usemodule_teardown()
.This is an alternative to using
ConfiguringTestBase
; the two should generally not be mixed in a module. It can also be used with Nose’swith_setup
function.
- nti.testing.base.module_teardown()[source]#
Tears down the module-level fixture for configuring packages established by
module_setup()
.Either import this as
tearDownModule
at the module level, or call it to perform module level tear down froum your own function with that name.This is an alternative to using
ConfiguringTestBase
; the two should generally not be mixed in a module.
Clean up things that should be cleared for every test, even in a shared test base.