Test-More / Test2-Harness

Alternative to Test::Harness
Other
23 stars 26 forks source link

All preload stages always run? #239

Closed JRaspass closed 2 years ago

JRaspass commented 2 years ago

I'm trying to split my test suite into unit and service tests with one common preloader. I would like the heavy preloads of service tests not to run when just running unit tests.

The following marks the test as stage DEFAULT via both a comment in the test file and the file_stage callback but it still seems to run the HEAVY stage and I don't see why:

Preload.pm

package Preload;

use Test2::Harness::Runner::Preload;

file_stage sub { 'DEFAULT' };

stage DEFAULT => sub { default };

stage HEAVY => sub { preload sub { warn "HEAVY\n" } };

1;

foo.t

# HARNESS-STAGE-DEFAULT

use Test2::V0;

pass;

done_testing;
$ yath test --include . --preload Preload foo.t
(INTERNAL)     HEAVY
( PASSED )  job  1    foo.t
exodist commented 2 years ago

All stages always load. This is by design. Yath cannot always know what stages will be needed. file_stage can be used to override what files provide, but due to the same issue as the halting problem, it cannot always know that 'DEFAULT' is the only possible stage in your case.

For your use case there are 2 possibilities: 1) Add an env var, and make the Preload module only define stages based on what you set the env var to. 2) Create a base class with common logic, and 2 subclasses that use it, one for unit tests, one for service tests, then pick the right one every time/place you run tests. IE have a services preloader and a unit preloader instead of one single one, but avoid duplicated effort by having both use common logic.

JRaspass commented 2 years ago

Ah thanks, I was trying to get away with putting the preload class in the config file and having it work out which logic to run, instead I'll just pass the appropriate class on the CLI, not a huge deal.