Perl Test::更多和设置测试要求

发布于 2024-09-18 01:34:19 字数 1301 浏览 3 评论 0原文

对于Test::More,我经常希望有一个模块可以运行测试并能够中止调用者test_plan。我进行了一系列测试,为 Catalyst::Test 设置了插件列表。我不想进行测试来检查它们是否存在;相反,如果这些插件不存在,我希望我的脚本中止。

我试图找出 Catalyst 中的错误::Authentication::Store::DBI::ButMaintained,我注意到这个错误也存在于 Catalyst::Authentication::Store::DBI。如下:

eval {
  require Catalyst::Model::DBI;
  require Catalyst::Plugin::Session;
  require Catalyst::Plugin::Session::State::Cookie;
  require Catalyst::Plugin::Session::Store::File;
  require DBD::SQLite;
  require Test::WWW::Mechanize::Catalyst;
} or plan skip_all => $@;

...

$ENV{'TESTAPP_PLUGINS'} = [ qw(
  Authentication
  Session
  Session::Store::File
  Session::State::Cookie
  Authorization::Roles
) ];

如您所见,eval/skip_all 不会检查 Authorization::Roles 包含,但测试依赖于它,因为它是一个插件。

不过,我还有另一个问题——是否有比这更优雅的方式来指定测试依赖项?请记住,我的目标与原作者相同。如果测试要求不存在,我只想跳过测试。理想情况下,在这种情况下,我想破解 Catalyst::Test 来包装 Catalyst::Plugin::* 内容的插件机制,然后找到更好的无需 eval/skip_all 即可完成其余操作的方法。

With Test::More I often want to have a module that runs tests and has the ability to abort the callers test_plan. I have a series of tests that set up a plugin list for Catalyst::Test. I don't want to have to make my test check to see if they exist; instead, I want my script to abort if those plugins aren't present.

I was trying to track down a bug in my Catalyst::Authentication::Store::DBI::ButMaintained, and I noticed this bug is also present in Catalyst::Authentication::Store::DBI. Here it is:

eval {
  require Catalyst::Model::DBI;
  require Catalyst::Plugin::Session;
  require Catalyst::Plugin::Session::State::Cookie;
  require Catalyst::Plugin::Session::Store::File;
  require DBD::SQLite;
  require Test::WWW::Mechanize::Catalyst;
} or plan skip_all => $@;

...

$ENV{'TESTAPP_PLUGINS'} = [ qw(
  Authentication
  Session
  Session::Store::File
  Session::State::Cookie
  Authorization::Roles
) ];

As you can see, the eval/skip_all doesn't check Authorization::Roles inclusion, but the test depends on it by virtue of it being a plugin.

I have another question though -- is there a more elegant way to specify Test-dependencies than this? Keep in mind my goal is the same as the original authors. I simply want to skip the test, if the test requirements don't exist. Ideally, in this case, I'd like to hack Catalyst::Test to wrap the plugin mechanism for Catalyst::Plugin::* stuff, and then find a better way to do the rest of this stuff without eval/skip_all.

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

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

发布评论

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

评论(2

你爱我像她 2024-09-25 01:34:20

您可以通过调用动态影响计划中的测试数量:

Test::More->builder->plan(tests=>$total_tests);

您可以使用 do 根据需要有条件地计算测试数量。

You can dynamically affect the # of tests in the plan by calling:

Test::More->builder->plan(tests=>$total_tests);

You can use that do conditionally calculate the # of tests based on the needs.

茶花眉 2024-09-25 01:34:20

根据需要更新您的插件列表:

如果您在单独的包中测试一堆要求,如果不满足依赖项,您可以简单地让该包返回 false(而不是传统的 true 值):

package Catalyst::Test;

eval {
    use Dep1;
    use Dep2;
    # ...
}

# dep check package returns true if we found all the modules
!$@;

# test.pl
use Test::Requires {
    Catalyst::Test => 0.01,  # skip all tests if Catalyst::Test is not present
};
use Test::More tests => 20;  # call Test::More as normal.

当我使用名为 Foo 的 dep 检查器,此操作失败并显示适当的输出:

% perl -I. foo.t
1..0 # SKIP Foo.pm did not return a true value at (eval 4) line 2.
# BEGIN failed--compilation aborted at (eval 4) line 2.
#
% prove -I. foo.t
foo.pl .. skipped: Foo.pm did not return a true value at (eval 4) line 2.
Files=1, Tests=0,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.01 cusr  0.00 csys =  0.04 CPU)
Result: NOTESTS

Update with your list of plugins as needed:

If you are testing a bunch of requirements in a separate package, you could simply have that package return false (rather than the traditional true value) if a dependency is not met:

package Catalyst::Test;

eval {
    use Dep1;
    use Dep2;
    # ...
}

# dep check package returns true if we found all the modules
!$@;

# test.pl
use Test::Requires {
    Catalyst::Test => 0.01,  # skip all tests if Catalyst::Test is not present
};
use Test::More tests => 20;  # call Test::More as normal.

When I run this using a dep checker called Foo, this fails with appropriate output:

% perl -I. foo.t
1..0 # SKIP Foo.pm did not return a true value at (eval 4) line 2.
# BEGIN failed--compilation aborted at (eval 4) line 2.
#
% prove -I. foo.t
foo.pl .. skipped: Foo.pm did not return a true value at (eval 4) line 2.
Files=1, Tests=0,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.01 cusr  0.00 csys =  0.04 CPU)
Result: NOTESTS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文