仅当安装了所需的模块时,如何才能在 Perl 模块的测试套件中运行测试?

发布于 2024-12-20 16:56:38 字数 217 浏览 1 评论 0原文

我想向我的 Perl 发行版添加一个需要模块 Foo 的测试,但我的发行版不需要 Foo;只有测试需要 Foo。所以我不想将模块添加到依赖项中,而是如果 Foo 在构建时不可用,我只想跳过需要 Foo 的测试。

执行此操作的正确方法是什么?我是否应该将 Foo 测试与 use Foo; 一起包装在 eval 块中,以便在加载 Foo 失败时测试不会运行?或者有更优雅的方法吗?

I want to add a test to my Perl distribution that requires a module Foo, but my distribution does not require Foo; only the test requires Foo. So I don't want to add the module to the dependencies, but instead I just want to skip the tests that require Foo if Foo is not available at build time.

What is the proper way to do this? Should I just wrap my Foo tests in an eval block along with use Foo;, so that the tests will not run if loading Foo fails? Or is there a more elegant way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静待花开 2024-12-27 16:56:38

如果需要 Some::Module 的所有测试都在一个文件中,那么很容易做到:(

use Test::More;

BEGIN {
    eval {
        require Some::Module;
        1;
    } or do {
        plan skip_all => "Some::Module is not available";
    };
}

如果您使用像 use Test::Moretests => 这样的测试计数; 42; 那么您还需要安排进行计划测试 => 42;(如果要求成功)。

)包含其他内容的文件中的测试,然后你可以这样做:

our $HAVE_SOME_MODULE = 0;

BEGIN {
    eval {
        require Some::Module;
        $HAVE_SOME_MODULE = 1;
    };
}

# ... some other tests here

SKIP: {
    skip "Some::Module is not available", $num_skipped unless $HAVE_SOME_MODULE;
    # ... tests using Some::Module here
}

If all of the tests that require Some::Module are in a single file, it's easy to do:

use Test::More;

BEGIN {
    eval {
        require Some::Module;
        1;
    } or do {
        plan skip_all => "Some::Module is not available";
    };
}

(If you're using a test count like use Test::More tests => 42; then you need to also arrange to do plan tests => 42; if the require does succeed.)

If they're a smaller number of tests in a file that contains other stuff, then you could do something like:

our $HAVE_SOME_MODULE = 0;

BEGIN {
    eval {
        require Some::Module;
        $HAVE_SOME_MODULE = 1;
    };
}

# ... some other tests here

SKIP: {
    skip "Some::Module is not available", $num_skipped unless $HAVE_SOME_MODULE;
    # ... tests using Some::Module here
}
失去的东西太少 2024-12-27 16:56:38

如果不满足某些条件,Test::More 可以选择跳过,请参见下文

SKIP: {
    eval { require Foo };

    skip "Foo not installed", 2 if $@;

    ## do something if Foo is installed
};

Test::More has an option to skip if some condition is not satisfied, see below

SKIP: {
    eval { require Foo };

    skip "Foo not installed", 2 if $@;

    ## do something if Foo is installed
};
勿忘心安 2024-12-27 16:56:38

Test::More 的文档中:

SKIP: {
    eval { require HTML::Lint };
    skip "HTML::Lint not installed", 2 if $@;
    my $lint = new HTML::Lint;
    isa_ok( $lint, "HTML::Lint" );
    $lint->parse( $html );
    is( $lint->errors, 0, "No errors found in HTML" );
}

From the documentation of Test::More:

SKIP: {
    eval { require HTML::Lint };
    skip "HTML::Lint not installed", 2 if $@;
    my $lint = new HTML::Lint;
    isa_ok( $lint, "HTML::Lint" );
    $lint->parse( $html );
    is( $lint->errors, 0, "No errors found in HTML" );
}
第七度阳光i 2024-12-27 16:56:38

另外,声明您的测试步骤要求或建议(存在差异)发行版元文件。这将由执行安装的客户端获取。在安装时,用户可以决定是永久安装这样的需求还是放弃它,因为它仅用于测试。

Also, declare your test step requirement or recommendation (there's a difference) in the distro meta file. This will be picked up by a client performing the installation. At install time, the user can decide whether to install such a requirement permanently or discard it because it was only used for testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文