Replies: 7 comments
-
You do not need to change whole provider. Just add new one overriding something. You need to create new container, but you can still use old providers. But I want to draw your attention to the types of tests you are writing: you do not need container other than pytest in unit tests |
Beta Was this translation helpful? Give feedback.
-
Our providers can be big and have a lot of dependencies, so it is not cool to add new class declaration with overriding some methods, what do you mean by "you do not need container other than pytest in unit tests"? It could be cool to have a container that can be mocked in runtime (for tests only) and then have DI from that container in pytest automatically |
Beta Was this translation helpful? Give feedback.
-
Sorry, I did not get the problem. Let's suppose you have production class MockProvider(Provider):
db = provide(MockDbGateway, provides=DatabaseGateway)
test_cotnainer = make_container(my_prod_provider, MockProvider()) By saying "you do not need container other than pytest in unit tests" I mean that dishka container is a thing which you use to integrate all parts of an application together. If you are doing a unit test for some class you can pass mocked dependencies directly. It is easier and more flexible. You do not need hierarchy of objects here - leave it for integration tests. But, you can use pytest fixtures here as it bases on names and you can have lots of mocked versions and use them in different tests in different cobinations. |
Beta Was this translation helpful? Give feedback.
-
Is pytest_mock working with dishka? |
Beta Was this translation helpful? Give feedback.
-
I have no idea what is the purpose of |
Beta Was this translation helpful? Give feedback.
-
Can I help with anything else related to the topic of the issue? |
Beta Was this translation helpful? Give feedback.
-
That's it, can it be moved to discussions? |
Beta Was this translation helpful? Give feedback.
-
In docs we have a page with testing example. But, actually, in real tests we have a lot of dependencies and we need to make a lot of mocks, so it is not convenient to re-write fully mocked provider (also sometimes we need to mock some methods in certain tests and have it unmocked in others). Maybe something like dependency_override method could be implemented or there are some other best practices for testing with dishka (mocking dishka, support FromDishka in pytest)?
Beta Was this translation helpful? Give feedback.
All reactions