Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: explain DI container teardown in pytest fixtures #130

Merged
merged 6 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/introduction/application-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ from that_depends import BaseContainer, providers

class DIContainer(BaseContainer):
settings = providers.Singleton(Settings)
settings_casted: Settings = settings.cast
some_factory = providers.Factory(SomeFactory, service_name=settings_casted.service_name)
some_factory = providers.Factory(SomeFactory, service_name=settings.cast.service_name)
```

And when `some_factory` is resolved it will receive `service_name` attribute from `Settings`
2 changes: 1 addition & 1 deletion docs/providers/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- generator or async generator can be used;
- context manager derived from `typing.ContextManager` or `typing.AsyncContextManager` can be used;

# How it works
## How it works
```python
import typing

Expand Down
21 changes: 21 additions & 0 deletions docs/testing/fixture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Fixture
## Dependencies teardown
When using dependency injection in tests, it's important to properly tear down resources after tests complete.

Without proper teardown, the Python event loop might close before resources have a chance to shut down properly, leading to errors like `RuntimeError: Event loop is closed`.

You can set up automatic teardown using a pytest fixture:

```python
import pytest_asyncio
from typing import AsyncGenerator

from my_project import DIContainer

@pytest_asyncio.fixture(autouse=True)
async def di_container_teardown() -> AsyncGenerator[None]:
try:
yield
finally:
await DIContainer.tear_down()
```
Loading