| [ Web Proxy ] |
| Viewing: https://python-injection.remimd.dev/testing/classes/ | [Back] [Original] |
Test frameworks like pytest and unittest don't allow custom __init__ methods in test classes. To inject dependencies into test classes, use LazyInstance for sync dependencies and aget_lazy_instance for async dependencies.
The LazyInstance descriptor resolves dependencies on access:
from injection import LazyInstance
class TestFeature:
dependency = LazyInstance(Dependency)
def test_something(self):
result = self.dependency.some_method()
assert result == expected_value
For async dependencies, use aget_lazy_instance which returns an awaitable:
from injection import aget_lazy_instance
class TestAsyncFeature:
lazy_dependency = aget_lazy_instance(AsyncDependency)
async def test_something(self):
dependency = await self.lazy_dependency
result = await dependency.some_method()
assert result == expected_value
Tip
Use LazyInstance for sync dependencies (cleaner syntax) and aget_lazy_instance only when you need async dependencies.
Warning
LazyInstance resolves the dependency on every access. With transient dependencies, you get a new instance each time. With singletons, you get the same instance.
| Web Proxy Viewer | New URL | Original Page |