perl-Test-Spec icon indicating copy to clipboard operation
perl-Test-Spec copied to clipboard

Accessing the test name from within the test subroutine

Open billmatzen opened this issue 8 years ago • 6 comments

I'm finding the Test::Spec module very useful, but I've been trying to find a way to access the test name from within the test subroutine, but have so far been unable to find a good way. I was hoping to find a magic variable being passed to the subroutine or a global value holding the current test case, but haven't found anything. My purpose would be for error logging from within the test case code (such as beginning, end, and just for general context).

I realize I could do the following, but 'd like to avoid repeating the test case name in every case:

it "My Test Name" => sub {
    my $test_case_name = "My Test Name";
    print "ENTER $test_case_name\n";
    ok(1);
    print "EXIT  $test_case_name\n";
};

ENTER My Test Name
ok 1 - My Test Name 
EXIT  My Test Name

I tried the following, but if there are multiple tests that all use the same file scope variable (as below), the value at runtime is the last value it was set to ("My Test Name 2"), so that doesn't work very well.

my $test_case_name = "My Test Name 1";
it $test_case_name => sub {
    print "ENTER $test_case_name\n";
    ok(1);
    print "EXIT  $test_case_name\n";
};

$test_case_name = "My Test Name 2";
it $test_case_name => sub {
    print "ENTER $test_case_name\n";
    ok(1);
    print "EXIT  $test_case_name\n";
};

ENTER **My Test Name 2**
ok 1 - My Test Name 1
EXIT  **My Test Name 2**
ENTER My Test Name 2
ok 2 - My Test Name 2
EXIT  My Test Name 2

Thanks for any advice, or if this is a feature request, let me know how to submit that.

Thanks, Bill

billmatzen avatar Aug 10 '17 17:08 billmatzen

Hey Bill, each it block receives a single parameter, a Test::Spec::Example instance. You can interrogate that object for the test name and description:

describe "Test::Spec" => sub {
  describe "example" => sub {
    it "should know its name" => sub {
      my $self = shift;
      is($self->name, "t1_test_spec_example_should_know_its_name")
    };

    it "should know its description" => sub {
      my $self = shift;
      is($self->description, "Test::Spec example should know its description");
    };
  };
};

kingpong avatar Aug 11 '17 01:08 kingpong

Hi Philip, thanks for getting back to me so quickly! :)

I was encouraged to hear it was this easy, but when I tried it out, I keep getting undef for the first argument. I tried with your example and get the following:

not ok 1 - Test::Spec example should know its name

Failed test 'Test::Spec example should know its name' by dying:

Can't call method "name" on an undefined value

at Specs/Test.pm line 15, <GEN0> line 5.

not ok 2 - Test::Spec example should know its description

Failed test 'Test::Spec example should know its description' by dying:

Can't call method "description" on an undefined value

at Specs/Test.pm line 20, <GEN0> line 7.

I'm using version 0.51 of course.

I don't feel like I'm doing anything abnormal. If you have a small working example could you send it, or would you prefer I send my own concise example file(s) that isn't working for me?

Thanks, Bill


From: Philip Garrett [email protected] Sent: Thursday, August 10, 2017 6:57 PM To: kingpong/perl-Test-Spec Cc: Matzen, Bill; Author Subject: Re: [kingpong/perl-Test-Spec] Accessing the test name from within the test subroutine (#41)

Hey Bill, each it block receives a single parameter, a Test::Spec::Example instance. You can interrogate that object for the test name and description:

describe "Test::Spec" => sub { describe "example" => sub { it "should know its name" => sub { my $self = shift; is($self->name, "t1_test_spec_example_should_know_its_name") };

it "should know its description" => sub {
  my $self = shift;
  is($self->description, "Test::Spec example should know its description");
};

}; };

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/kingpong/perl-Test-Spec/issues/41#issuecomment-321716986, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AVl7PEeVr3tkk-8sv0SFH8-HggOFIqACks5sW7T6gaJpZM4OzuVg.

billmatzen avatar Aug 11 '17 15:08 billmatzen

Sorry Bill, I haven't been active in this codebase for a while (someone else is maintaining it) and I didn't realize that's an unreleased feature. If you run it against develop branch it'll work. I'm not sure if it was held back intentionally.

@andyjones any insight?

kingpong avatar Aug 12 '17 14:08 kingpong

This is great, I've tested the develop branch and it works great. Can this be merged into the master branch? I'd love to start using this.

billmatzen avatar Aug 14 '17 15:08 billmatzen

I've released it to cpan as 0.52.

Originally I wanted to release it after I had added some docs. I'll leave this issue open to remind me

andyjones avatar Aug 15 '17 10:08 andyjones

Thank you so much - it's working great! :)

billmatzen avatar Aug 15 '17 17:08 billmatzen