【问题标题】:How do I test my module that uses File::ShareDir?如何测试使用 File::ShareDir 的模块?
【发布时间】:2012-03-12 16:32:39
【问题描述】:

我正在编写一个使用File::ShareDir::Install 在 dist 目录中安装共享文件的模块。在模块的主要导出函数中,然后使用 File::ShareDir 定位文件。

此函数加载共享文件,并在文件中查找字符串。在我的一项测试中,我想检查函数是否为一些输入返回了正确的值。所以,测试看起来像:

my @cases = (
    [ 'input1' => 'expected1' ],
    # etc
);

for my $case ( @cases ) {
    my ($input, $expected) = @$case;
    is(
        my_lookup_function($input),
        $expected,
        "$input => $expected",
    );
}

但是,测试没有运行,因为

my $dir = dist_dir('My-Dist');

呱呱叫。

我的Makefile.PL似乎是正确的,因为我可以在blib下找到共享文件。我只是不知道我需要做什么才能在我的模块中调用File::ShareDir::dist_dir 以在测试期间找到这个目录。

我确实看过Test::File::ShareDir,但我不明白它是否可以用来做我想做的事情。

如果您能告诉我如何使用当前设置实现所需的行为,或者建议以不同的方式做事(并且可能是正确的;-)

,我将不胜感激

【问题讨论】:

    标签: perl testing perl-module


    【解决方案1】:

    为我工作。

    perl Makefile.PL ; make
    make test
    prove -b
    

    $ tree
    .
    ├── Makefile.PL
    ├── share
    │   └── quux
    └── t
        └── foo.t
    
    2 directories, 3 files
    
    $ cat Makefile.PL
    use ExtUtils::MakeMaker;
    use File::ShareDir::Install;
    
    install_share 'share';
    WriteMakefile;
    
    package MY;
    use File::ShareDir::Install qw(postamble);
    
    
    $ cat t/foo.t
    use File::ShareDir qw(dist_dir);
    use Test::More;
    
    diag dist_dir('My-Dist');
    
    pass;
    done_testing;
    

    【讨论】:

    • 哎呀!你是对的。我在测试文件中有一个不同的问题,它实际上正盯着我的脸。直到我阅读了您的答案并转移了注意力,我才注意到它。谢谢。
    【解决方案2】:

    您可以使用模块 Test::File::ShareDir 告诉您的测试在哪里查找您的 ShareDir 文件。

    来自文档:

    use Test::More;
    
    # use FindBin; optional
    
    use Test::File::ShareDir
        # -root => "$FindBin::Bin/../" # optional,
        -share => {
            -module => { 'My::Module' => 'share/MyModule' }
            -dist   => { 'My-Dist'    => 'share/somefolder' }
        };
    
    use My::Module;
    
    use File::ShareDir qw( module_dir dist_dir );
    
    module_dir( 'My::Module' ) # dir with files from $dist/share/MyModule
    
    dist_dir( 'My-Dist' ) # dir with files from $dist/share/somefolder
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2010-10-10
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多