如何在Perl中进行单元测试?

时间:2021-11-28 12:14:59

I have been doing some OO Perl programming and I was wondering: which is the best way to perform unit tests?

我一直在做一些OO Perl编程,我想知道:哪个是执行单元测试的最佳方法?

So far I have been using the Test::Simple module to perform tests, but it feels insufficient for what I want.

到目前为止,我一直在使用Test :: Simple模块来执行测试,但感觉不足以满足我的需求。

Can you point me to some nice modules for that?

你能指点我一些很好的模块吗?

5 个解决方案

#1


48  

I'd add my vote to picking up Test::More before going any further in Perl testing. The Perl testing community is fairly well united around the Test Anything Protocol, and you'll want to play around with Test::More to understand how it works and how tools like prove and Test::Harness::Archive can help automate and distribute testing.

在Perl测试中再进行任何进一步的测试之前,我会加入我的投票来获取Test :: More。 Perl测试社区在测试任何协议方面相当完美,您将想要使用Test :: More来了解它是如何工作的,以及如何使用证明和Test :: Harness :: Archive等工具来帮助自动化和分发测试。

If you want to just "jump right in", I think Test::Class provides xTest facilities with a TAP backend. I haven't used it at all (I'm a Test::More person myself), but it's very highly rated.

如果你想“直接进入”,我认为Test :: Class为xTest工具提供了一个TAP后端。我根本没有使用它(我是一个测试::更多人自己),但它的评价非常高。

#2


18  

Test::More should offer you more bang for your bucks once you get the hang of Test::Simple.

测试::一旦你掌握了Test :: Simple,更多应该为你的雄鹿提供更多的帮助。

Also you can refer to this previous discussion by yours truly if you want more info: how-can-i-implement-tdd-in-perl

如果您想了解更多信息,也可以参考之前的讨论:how-can-i-implement-tdd-in-perl

#3


12  

Judging by your comments on melaos answer, I'd say Test::Class or Test::Unit is what you're looking for.

根据你对melaos回答的评论,我会说Test :: Class或Test :: Unit就是你想要的。

#4


11  

Simple test example:

简单的测试示例:

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Test::More plan => 4;  # or use Test::More 'no_plan';

use_ok('My::Module', 'Loaded My::Module');
ok( my $obj = My::Module->new(), 'Can create instance of My::Module');

ok( $obj->value('hello'), 'Set value to hello' );
is( $obj->value => 'hello', 'value is still hello');

#5


3  

Test::Class usage you can see in this example.

您可以在此示例中看到Test :: Class用法。

#1


48  

I'd add my vote to picking up Test::More before going any further in Perl testing. The Perl testing community is fairly well united around the Test Anything Protocol, and you'll want to play around with Test::More to understand how it works and how tools like prove and Test::Harness::Archive can help automate and distribute testing.

在Perl测试中再进行任何进一步的测试之前,我会加入我的投票来获取Test :: More。 Perl测试社区在测试任何协议方面相当完美,您将想要使用Test :: More来了解它是如何工作的,以及如何使用证明和Test :: Harness :: Archive等工具来帮助自动化和分发测试。

If you want to just "jump right in", I think Test::Class provides xTest facilities with a TAP backend. I haven't used it at all (I'm a Test::More person myself), but it's very highly rated.

如果你想“直接进入”,我认为Test :: Class为xTest工具提供了一个TAP后端。我根本没有使用它(我是一个测试::更多人自己),但它的评价非常高。

#2


18  

Test::More should offer you more bang for your bucks once you get the hang of Test::Simple.

测试::一旦你掌握了Test :: Simple,更多应该为你的雄鹿提供更多的帮助。

Also you can refer to this previous discussion by yours truly if you want more info: how-can-i-implement-tdd-in-perl

如果您想了解更多信息,也可以参考之前的讨论:how-can-i-implement-tdd-in-perl

#3


12  

Judging by your comments on melaos answer, I'd say Test::Class or Test::Unit is what you're looking for.

根据你对melaos回答的评论,我会说Test :: Class或Test :: Unit就是你想要的。

#4


11  

Simple test example:

简单的测试示例:

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Test::More plan => 4;  # or use Test::More 'no_plan';

use_ok('My::Module', 'Loaded My::Module');
ok( my $obj = My::Module->new(), 'Can create instance of My::Module');

ok( $obj->value('hello'), 'Set value to hello' );
is( $obj->value => 'hello', 'value is still hello');

#5


3  

Test::Class usage you can see in this example.

您可以在此示例中看到Test :: Class用法。