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:
objectA 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 theConfigurationMachinethat 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.eventtestingmodule will be configured.Note
If there are any
set_up_packagesyou are responsible for ensuring that thezope.componentconfiguration 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
devmodeandtestmode. (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
testsin a module beginning withtest, this defaults to the parent package. E.g., if klass isnti.appserver.tests.test_app.TestAppthen 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.zcmlpackage 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,TestCaseBase 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_GCis 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.sitefor details.) This isFalseby 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_GCis true.
- class nti.testing.base.AbstractTestBase(methodName='runTest')[source]¶
Bases:
CleanUp,PatchingMixin,TestCaseBase 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,AbstractTestBaseTest 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
setUpmethod of a subclass. Note that this is called bysetUp.
- class nti.testing.base.PatchingMixin[source]¶
Bases:
objectMixin class adding support for dynamic
unittest.mockpatches.Added in version 4.0.0.
- patch(*args, **kwargs)[source]¶
API for subclasses. All args are passed through to
unittest.mock.patchwhich is then started and registered for cleanup.This is intended to be used in
setUpor individual test methods when what you might need to patch is dynamic.Returns the result of
patch.start(), i.e., a mock object.Added in version 4.0.0.
Bases:
AbstractConfiguringObject,AbstractSharedTestBaseTest case that can be subclassed when ZCML configuration is desired.
Configuration is established by the class attributes documented on
AbstractConfiguringObject. (Theconfiguration_contextis 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
setUpClassmethod of a subclass after calling the super class. Note that this is called bysetUpClass.
-
Added 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_GCis 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.sitefor details.) This isFalseby 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_GCis true.
Bases:
typeA metaclass that converts the nose-specific use of
setUpClassandtearDownClassinto 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
setUpModuleat 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_setupfunction.
- nti.testing.base.module_teardown()[source]¶
Tears down the module-level fixture for configuring packages established by
module_setup().Either import this as
tearDownModuleat 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.