帮助用户用 Perl 测试框架编写、运行与改进自动化测试及覆盖率分析。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "perl-testing" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/perl-testing/SKILL.md 2. 保存为 ~/.claude/skills/perl-testing/SKILL.md 3. 装好后重载技能,告诉我可以用了
请为这个 Perl 模块编写基于 Test2::V0 的单元测试,覆盖正常输入、边界条件和异常情况,并解释每组测试的目的。
一套可运行的 Perl 单元测试代码,并附带测试设计说明。
我有一组使用 Test::More 的旧 Perl 测试,请帮我迁移到 Test2::V0,保留原有断言语义,并指出可以改进的结构。
迁移后的测试文件、兼容性说明以及重构建议。
请为 Perl 项目设计一个测试驱动开发流程,包含 prove 的运行方式、mock 的使用建议,以及用 Devel::Cover 生成覆盖率报告的步骤。
一份清晰的测试流程方案,含命令示例、工具建议和覆盖率实践。
Comprehensive testing strategies for Perl applications using Test2::V0, Test::More, prove, and TDD methodology.
Always follow the RED-GREEN-REFACTOR cycle.
# Step 1: RED — Write a failing test
# t/unit/calculator.t
use v5.36;
use Test2::V0;
use lib 'lib';
use Calculator;
subtest 'addition' => sub {
my $calc = Calculator->new;
is($calc->add(2, 3), 5, 'adds two numbers');
is($calc->add(-1, 1), 0, 'handles negatives');
};
done_testing;
# Step 2: GREEN — Write minimal implementation
# lib/Calculator.pm
package Calculator;
use v5.36;
use Moo;
sub add($self, $a, $b) {
return $a + $b;
}
1;
# Step 3: REFACTOR — Improve while tests stay green
# Run: prove -lv t/unit/calculator.t
The standard Perl testing module — widely used, ships with core.
use v5.36;
use Test::More;
# Plan upfront or use done_testing
# plan tests => 5; # Fixed plan (optional)
# Equality
is($result, 42, 'returns correct value');
isnt($result, 0, 'not zero');
# Boolean
ok($user->is_active, 'user is active');
ok(!$user->is_banned, 'user is not banned');
# Deep comparison
is_deeply(
$got,
{ name => 'Alice', roles => ['admin'] },
'returns expected structure'
);
# Pattern matching
like($error, qr/not found/i, 'error mentions not found');
unlike($output, qr/password/, 'output hides password');
# Type check
isa_ok($obj, 'MyApp::User');
can_ok($obj, 'save', 'delete');
done_testing;
use v5.36;
use Test::More;
# Skip tests conditionally
SKIP: {
skip 'No database configured', 2 unless $ENV{TEST_DB};
my $db = connect_db();
ok($db->ping, 'database is reachable');
is($db->version, '15', 'correct PostgreSQL version');
}
# Mark expected failures
TODO: {
local $TODO = 'Caching not yet implemented';
is($cache->get('key'), 'value', 'cache returns value');
}
done_testing;
Test2::V0 is the modern replacement for Test::More — richer assertions, better diagnostics, and extensible.
use v5.36;
use Test2::V0;
# Hash builder — check partial structure
is(
$user->to_hash,
hash {
field name => 'Alice';
field email => match(qr/\@example\.com$/);
field age => validator(sub { $_ >= 18 });
# Ignore other fields
etc();
},
'user has expected fields'
);
# Array builder
is(
$result,
array {
item 'first';
item match(qr/^second/);
item DNE(); # Does Not Exist — verify no extra items
},
'result matches expected list'
);
# Bag — order-independent comparison
is(
$tags,
bag {
item 'perl';
item 'testing';
item 'tdd';
},
'has all required tags regardless of order'
);
use v5.36;
use Test2::V0;
subtest 'User creation' => sub {
my $user = User->new(name => 'Alice', email => '[email protected]');
ok($user, 'user object created');
is($user->name, 'Alice', 'name is set');
is($user->email, '[email protected]', 'email is set');
};
subtest 'User validation' => sub {
my $warnings = warns {
User->new(name => '', email => 'bad');
};
ok($warnings, 'warns on invalid data');
};
done_testing;
use v5.36;
use Test2::V0;
# Test that code dies
like(
dies { divide(10, 0) },
…
通过双评审智能体对结果进行对抗式校验,提升输出发布前的可靠性
帮助你系统掌握 Rust 测试模式与 TDD 实践,提升代码质量与可维护性。