Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add a function like subtest that returns a code ref that'll execute the subtests when called and pass args #346

Closed
timbunce opened this Issue Feb 7, 2013 · 7 comments

Comments

Projects
None yet
4 participants

timbunce commented Feb 7, 2013

For example, I would like to run test_psgi tests as subtests but I can't see a way to do that currently. I'm looking for something like this:

test_psgi $app, subtest_x "name for subtests" => {
     my $cb  = shift;
     my $res = $cb->(GET "/");
     is $res->content, "Hello";
};

It probably just requires subtest() to pass any extra args through to the code ref, and then subtest_x (which needs a better name) would simply return a closure that calls subtest() with the name plus any args it was called with.

Contributor

pdl commented Feb 7, 2013

Unless I'm missing something, you don't need Test::More to do anything differently to achieve that, you can just implement it in your code immediately:

sub subtest_x {
  return sub { subtest @_ };
}

timbunce commented Feb 7, 2013

That's what I tried first. It doesn't handle arguments.

The central problem is really that subtest doesn't enable passing of arguments to the subtest coderef when it's called. It's always called with no args.

Fixing that (which ought to be trivial) would enable a solution like yours to be used by those who need it.

Contributor

pdl commented Feb 7, 2013

Oh, I see!

How about:

sub subtest_x {
  my ($name, $code) = @_;
  return sub { my @args = @_; subtest $name, sub{$code->(@args); };  };
}
Owner

schwern commented Feb 8, 2013

There might be utility here for providing a "deferred subtest" concept.
That would turn tests into better functional units which can be passed
around and reused.

@timbunce has the interface. @pdl has an implementation. Before it
goes into Test::More or Test::Builder I'd like to hear some more about
what sorts of things it might be used for.

Contributor

pdl commented Feb 26, 2013

I'm doing stuff with Test::Proto that might do this sort of thing, not sure if I'd use a precurried version to do it. My feeling is that the circumstances where you want to curry subtest is going to be rarer mostly 'advanced use' that goes beyond the range of what Test::More typically is aimed at, and therefore circumstances where we might legitimately expect an author to do it in their own code.

Owner

schwern commented Mar 4, 2013

@pdl It does look like Test::Proto covers this sort of thing and covers it better. $proto->try( sub { ... } ); allows it to be generic.

One change would be the icing on the cake: if each ->ok call were a subtest.

osfameron added a commit to osfameron/test-more that referenced this issue Apr 27, 2014

subtest accepts @args, passed to code-block
This would seem to be the required fix to #346.  Accepting args allows
you to build testing routines like timbunce's `test_psgi` and my
test-dbic-calls.

osfameron added a commit to osfameron/test-more that referenced this issue Apr 27, 2014

subtest accepts @args, passed to code-block
This would seem to be the required fix to #346.  Accepting args allows
you to build testing routines like timbunce's `test_psgi` and my
test-dbic-calls.

exodist added a commit that referenced this issue May 10, 2014

subtest accepts @args, passed to code-block
This would seem to be the required fix to #346.  Accepting args allows
you to build testing routines like timbunce's `test_psgi` and my
test-dbic-calls.

Conflicts:
	lib/Test/Builder.pm
Owner

exodist commented Oct 30, 2014

Closing this since we have the ability to pass in args now. The nitty gritty of the request can now be implemented in any Test::* module you want to write.

@exodist exodist closed this Oct 30, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment