Registry /
testing / testing-common-database
Install & Compatibility
Where this runs
tested against v2.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.030s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.028s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Database
✓ from testing.common.database import Database
Base class for database testing packages, intended for inheritance.
DatabaseFactory
✓ from testing.common.database import DatabaseFactory
Factory class for creating and caching database instances to optimize test setup time.
SkipIfNotInstalledDecorator
✓ from testing.common.database import SkipIfNotInstalledDecorator
Decorator to conditionally skip test cases if a required database command is not found.
get_unused_port
✓ from testing.common.database import get_unused_port
Utility function to find an available TCP port.
get_path_of
✓ from testing.common.database import get_path_of
Utility function to search for a command in the system's PATH.
This quickstart demonstrates how to create a custom database testing class by inheriting from `testing.common.database.Database` and how to use `DatabaseFactory` to cache initialized database instances, speeding up subsequent tests. The `tearDownModule` ensures the cached database is properly cleaned up.
import os
import unittest
from testing.common.database import Database, DatabaseFactory
class MyTestDatabase(Database):
# This is a minimal mock for demonstration; real implementation would
# manage a database server (e.g., PostgreSQL, MySQL).
def initialize(self):
# Simulate database initialization
print(f"[{self.name}] Initializing database...")
def get_data_directory(self):
# Return a temporary directory for data
return os.path.join(self.base_dir, 'data')
def get_server_commandline(self):
# No actual server command for this mock
return ['echo', 'Mock database server started']
def is_server_available(self):
# Always available for this mock
return True
def stop(self):
# Simulate stopping the database
print(f"[{self.name}] Stopping database...")
# Create a factory for the mock database, with caching enabled
# In a real scenario, this would be a specific database factory like PostgresqlFactory
MyCachedDatabase = DatabaseFactory(target_class=MyTestDatabase, cache_initialized_db=True)
# Ensure the cache is cleared after all tests in this module
def tearDownModule():
MyCachedDatabase.clear_cache()
class MyDatabaseTestCase(unittest.TestCase):
def setUp(self):
# Each test gets a fresh copy of the cached database
self.db = MyCachedDatabase()
print(f"Setting up test with database instance at: {self.db.data_dir}")
def tearDown(self):
self.db.stop()
print(f"Tearing down test: {self.db.data_dir}")
def test_database_interaction(self):
# Simulate interacting with the database
self.assertTrue(self.db.is_alive())
print("Performing test operations...")
self.assertIn('data', self.db.data_dir)
if __name__ == '__main__':
unittest.main()
Debug
Known issues
deprecatedThe library has not seen a release since October 2017 (version 2.0.3) and officially supports Python 3.4, 3.5, and 3.6. Using it with newer Python versions (3.7+) may lead to compatibility issues or unaddressed bugs.fixThoroughly test compatibility with your specific Python version and dependencies. Consider maintaining a fork or seeking alternative, more actively maintained testing utilities if critical issues arise.
affects: 2.0.3 and older, especially with Python > 3.6
gotchaThe `Database` class is designed as an abstract base class. Directly instantiating `testing.common.database.Database` without inheriting and overriding essential methods (e.g., `initialize`, `get_data_directory`, `get_server_commandline`) will result in `NotImplementedError` or incorrect behavior, as it lacks concrete implementation for database interaction.fixAlways subclass `testing.common.database.Database` and implement all necessary abstract methods to define how your specific database instance is managed during tests. Refer to derived libraries like `testing.postgresql` for implementation examples.
affects: All
gotchaWhen using `DatabaseFactory(cache_initialized_db=True)`, the initialized database is cached and reused across tests. Failing to call `factory.clear_cache()` in a `tearDownModule` or similar cleanup hook can lead to stale test data or unexpected interactions between unrelated test cases, especially if `on_initialized` is not idempotently handled or test data is not reset.fixEnsure `DatabaseFactory.clear_cache()` is reliably called at the appropriate scope (e.g., once after all tests in a module or suite) to reset the cached database and prevent test pollution. Also, ensure any `on_initialized` handler is idempotent or handles setup/teardown cleanly for cached instances.
affects: All
Upgrade
Version history
2.0.3latest on PyPI · released Oct 23, 2017
Audit
Dependencies
No dependency data recorded yet.