Re^7: Introspecting function signatures (Dependency Injection and Monkey Patching)
by eyepopslikeamosquito (Archbishop) on Mar 07, 2021 at 07:02 UTC
|
"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 injection
So 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
| [reply] |
|
I think this niche is mainly filled by conditional use/imports in dynamic languages (see if )
Monkey patching is only one other possibility here (that's replacing a sub/method at runtime), we also have eval and BEGIN blocks ... etc.
> In frustration, I searched all
that's "terminology injection" (aka die "brain overflow" ;-)
Like all these Java OO "design patterns" which are superfluous in Perl.
| [reply] [d/l] |
|
that's "terminology injection" (aka die "brain overflow" ;-)
Ha ha, I noticed a mind-boggling number of different ways to do this type of stuff in Perl.
For example, chromatic refers here
to a modernperlbooks article which gives the illustrative example below:
At its core, dependency injection is a formalization of the design principle "Don't hard-code your dependencies." Consider the code:
sub fetch
{
my ($self, $uri) = @_;
my $ua = LWP::UserAgent->new;
my $resp = $ua->get( $uri );
...
}
That's not bad code by any means, but it's a little too specific and a little less generic due to the presence of the literal string LWP::UserAgent.
That might be fine for your application, but that hard-coding introduces a coupling that can work against other uses of this code. Imagine testing this code, for example.
While you could use Test::MockObject to intercept calls to LWP::UserAgent's constructor, that approach manipulates Perl symbol tables to work around a hard coded dependency.
An alternate approach uses a touch of indirection to allow for greater genericity:
use Moose;
has 'ua', is => 'ro', default => sub { LWP::UserAgent->new };
sub fetch
{
my ($self, $uri) = @_;
my $ua = $self->ua;
my $resp = $ua->get( $uri );
...
}
Now the choice of user agent is up to the code which constructs this object.
If it provides a ua to the constructor, fetch() will use that. Otherwise, the default behavior is to use LWP::UserAgent as before.
Lots of stuff on CPAN too:
As usual, the hard part is knowing which is good and which is crap.
Please let us know of other cool CPAN offerings I've overlooked.
| [reply] [d/l] [select] |
|
Re^7: Introspecting function signatures
by jcb (Parson) on Mar 06, 2021 at 21:51 UTC
|
sub test_something {
my $tempdir = create_tempdir();
# ...
}
Then the framework only needs to iterate over the package stash and call all test_* functions. Which leads to a "is this a good idea?" question — this style of testing means that the test routines are included with the main code and will have to be compiled every time the module is loaded. Python reduces this overhead by automatically using its form of B::Bytecode/ByteLoader but Perl does not do that, so this style will add some incremental overhead to all use of any module using it. The traditional Perl style of separate test scripts completely avoids this last problem. | [reply] [d/l] [select] |
|
Not sure what Python is implementing with that pattern, but I know it doesn't have block-scope, OTOH nesting functions is easier there.
So many things which are
just blocks in Perl become named functions there, even lamda is useless for that.
But no need to copy their limitations.
My suggestion would be to apply attributes or explicit use
sub :test something {
my :TEMPDIR $tempdir = shift; # or
my :FIXTURE $tempdir = shift; # or
use fixture qw/$tempdir/;
#...
}
This offers plenty of possibilities at compile time and is more flexible than Python's approach.
edit
and I'd certainly put test-subs into their own package, if they lived in the same file.
| [reply] [d/l] [select] |
|
| [reply] |
|
This can indeed be a good solution. Given that perl will reliably call DESTROY when an object goes out of scope, this could ensure that whatever the object holds will be cleaned up. It is a bit more code to write as we need to call the function, but might be reasonable.
sub test_something {
my $tempdir = create_tempdir();
# ...
}
I don't know where the idea came from that in Python people include the tests in the main code. Maybe that was the case 10 years ago, but these days the standard is that tests are in separate files called test_SOMETHING.py.
| [reply] [d/l] |
|
| [reply] |
|
|
|