On Thursday 11 January 2007 06:30, Ovid wrote:
> Quite often people will write code which tests to see if
> $ENV{HARNESS_ACTIVE} is true. For example, this allows them to not
> email support from their code while testing. This variable is set in
> Test::Harness. However, this causes a problem when someone
> accidentally does this:
>
> perl t/email_support.t
>
> You can verify this behavior by running this with 'perl' and 'prove'.
> It will fail when run through Perl.
>
> use Test::More tests => 1;
> ok $ENV{HARNESS_ACTIVE}, 'running in the test harness';
Hm. You're asking for a general purpose way *in your test file* to tell *if
someone is running your test file*.
I wonder if it would be possible to put code *in your test file* that detects
when someone has run it.
(I know; it's not exactly what you were asking. I just wanted to get that in
a public mailing list so I could call that the "Star Trek: Generations"
fallacy. You steal a spaceship, which flies through space, to fly through
space to a planet, flying through space, where a temporal anomaly, which also
flies through space, deflected by a supernova, which you flew through space
in your spaceship which flies through space, passes close enough to the
planet, because both of them fly through space, that you can jump off a
bridge into it.)
I'm sympathetic to the argument that your test file is going to be ugly when
the code you're testing is ugly, but it seems to me that containing that
ugliness within the specific test file until you can refactor the test file
is much better than bolting another ad-hoc feature onto a testing system
which already makes way to many underspecified assumptions and would be
fairly difficult to replace with something nicer, someday.
It's not as if your test file won't be full of ugly code anyway, in this
situation. Encouraging other people to fill their code with ugly hacks seems
suboptimal. I sleep at night just fine thinking that having to work around
ugly code is painful. Perhaps it will encourage them to fix the ugliness.
It *should* hurt when you have different code paths in your application for
and against testing. My preference would be repeated electric shocks, but
I'll live with a general sense of nausea.
-- c