Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re^7: Introspecting function signatures (Dependency Injection and Monkey Patching)by eyepopslikeamosquito (Archbishop) |
on Mar 07, 2021 at 07:02 UTC ( [id://11129243]=note: print w/replies, xml ) | Need Help?? |
"dependency injection" is how this is called in the case of pytest, maybe it is not a generic term Well, I think Dependency Injection is a very well-known generic term, and heavily used in statically typed languages (e.g. C++, Java, C#) though it seems to be much less popular in dynamic languages, such as Perl and Python, as indicated here: Dependency injection (DI) is regarded with suspicion in the Python world ... The standard way to do things is to declare our dependency implicitly by simply importing it, and then if we ever need to change it for tests, we can monkeypatch, as is Right and True in dynamic languages.A reply to this SO question states that "A MonkeyPatch is a piece of Python code which extends or modifies other code at runtime (typically at startup)". In frustration, I searched all of docs.pytest.org and the only reference I found to dependency injection was: consider using pytest’s more powerful fixture mechanism which leverages the concept of dependency injectionSo please put me out of my misery by pointing us at the pytest dependency injection documentation you saw. BTW, I've happily used Dependency Injection for years in C++ (but not Perl). In C++, you define an interface argument to the class constructor, then, in the unit test, inject a mock object (an instance of a class inheriting from the interface class), while injecting an instance of the real class (also inheriting from the same interface class) in production code. ... Hmmmm, I see I liked it so much that I explicitly singled it out as "perhaps the most important design pattern in making code easier to test" at Effective Automated Testing. Update: See this reply for a nice example of using Dependency Injection in Perl by chromatic. Monkey Patching References
In Section
Seekers of Perl Wisdom
|
|