nti.testing.layers

Test layer support.

Changed in version 4.0.0: This is now a package with sub-modules. Existing imports continue to work.

nti.testing.layers.find_test()[source]

The layer support in nose2.plugins.layers.Layers optionally supplies the test case object to testSetUp and testTearDown, but zope.testrunner does not do this. If you need access to the test, you can use an idiom like this:

@classmethod
def testSetUp(cls, test=None):
    test = test or find_test()

Test layers for working with Zope libraries.

class nti.testing.layers.zope.ConfiguringLayerMixin[source]

Bases: AbstractConfiguringObject

Inherit from this layer at the leaf level to perform configuration. You should have already inherited from ZopeComponentLayer.

To use this layer, subclass it and define a set of packages. This should be done EXACTLY ONCE for each set of packages; things that add to the set of packages should generally extend that layer class. You must call setUpPackages() and tearDownPackages() from your setUp and tearDown methods.

See AbstractConfiguringObject for documentation on the class attributes to configure.

classmethod get_configuration_package()

Added in version 2.1.0.

classmethod setUpPackages()[source]

Set up the configured packages.

classmethod tearDownPackages()[source]

Tear down all configured packages in the global site manager.

class nti.testing.layers.zope.ZopeComponentLayer[source]

Bases: SharedCleanupLayer

Test layer that can be subclassed when zope.component will be used.

This does nothing but set up the hooks and the event handlers.

classmethod testSetUp()[source]

Calls sharedCleanup() for every test.

classmethod testTearDown()[source]

Calls sharedCleanup() for every test.

Support for cleaning up:

  • Garbage collection

  • Registered cleanups.

Added in version 4.0.0.

class nti.testing.layers.cleanup.GCLayerMixin[source]

Bases: object

Mixin this layer class and call setUpGC() from your layer setUp method and likewise for the teardowns when you want to do GC.

classmethod setUpGC()[source]

This method disables GC until tearDownGC() is called. You should call it from your layer setUp method.

It also cleans up the zope.testing state.

classmethod tearDownGC()[source]

This method executes zope.testing’s cleanup and then renables GC. You should call if from your layer tearDown method.

class nti.testing.layers.cleanup.SharedCleanupLayer[source]

Bases: object

Mixin this layer when you need cleanup functions that run for every test.

classmethod testSetUp()[source]

Calls sharedCleanup() for every test.

classmethod testTearDown()[source]

Calls sharedCleanup() for every test.

Support for using testgres to create and use a Postgres instance as a layer.

There is also support for benchmarking and saving databeses for later examination.

Added in version 4.0.0.

The APIs are preliminary and may change.

This is only supported on platforms that can install psycopg2.

class nti.testing.layers.postgres.DatabaseBackupLayerHelper[source]

Bases: object

A layer helper that works with another layer to

  • create a backup of the current database on push;

  • make that backup active;

  • switch the connection pool to that backup

  • reverse all of that on layer pop

Note that this consists of modifying values in the DatabaseLayer, so the layer parameter must extend that.

class nti.testing.layers.postgres.DatabaseLayer[source]

Bases: object

A test layer that creates the database, and sets each test up in its own connection, aborting the transaction when done.

DATABASE_NAME = 'postgres'

The name of the database within the node. We only create the default databases, so this should be ‘postgres’

classmethod borrowed_connection()[source]

Context manager that returns a connection from the connection pool.

connection = None

Set for each test.

connection_pool_klass

alias of ThreadedConnectionPool

cursor = None

Set for each test.

classmethod drop_relation(relation, kind='TABLE', idempotent=False)[source]

Drops the relation of type kind (default table), in new transaction.

postgres_dsn = None

A string you can use to connect to Postgres.

postgres_node = None

A testgres.node.PostgresNode, created for the layer. A psycopg2 connection to it is located in the connection attribute (similarly for connection_pool), while a DSN connection string is in postgres_dsn

postgres_uri = None

A string you can pass to SQLAlchemy

classmethod truncate_table(conn, table_name)[source]

Transactionally truncate the given table_name using conn

class nti.testing.layers.postgres.DatabaseTestCase(methodName='runTest')[source]

Bases: TestCase

A helper test base containing some functions useful for both benchmarking and unit testing.

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 nti.testing.layers.postgres.PersistentDatabaseLayer[source]

Bases: SchemaDatabaseLayer

A layer that establishes persistent data visible to all of its tests (and all of its sub-layers).

Sub-layers need to check whether they should clean up or not, because we may be saving the database file.

It’s important to have a fairly linear layer setup, or layers that don’t interfere with each other.

classmethod persistent_layer_skip_setup()[source]

Should persistent layers skip their setup because we loaded a save file?

classmethod persistent_layer_skip_teardown()[source]

Should persistent layers, that write data intended to be visible between tests (and in sub-layers) tear down that data when the layer is torn down? If we’re saving the database, we don’t want to do that.

Raising NotImplementedError causes the testrunner to assume it’s python resources that are the problem and continue in a new subprocess, which doesn’t help (and may hurt?). So you must check this as a boolean.

class nti.testing.layers.postgres.SchemaDatabaseLayer[source]

Bases: DatabaseLayer

A test layer that adds our schema.