views:

110

answers:

5

There's Test::Simple, Test::More, Test::Builder (all part of the Test::Simple distribution), Test::Class, Test::Unit, Test::Moose...

I'm starting a new project using Moose—which module should I use for writing my tests?

+19  A: 

You can eliminate Test::Builder from the list for a while. Test::Builder is the base module that other Test:: modules are built on. So until you want to start writing your own test modules you won't need it.

I'd also ignore Test::Simple. Test::More does all that Test::Simple does - and plenty more.

Test::Class is a nice way to write unit tests in a really object oriented way. I'd recommend it for complex OO-based systems.

Test::Moose is for testing various Moose-related features in your code. You say that you're using Moose so it might well be useful for you. It can be used in conjunction with Test::More.

So my recommendation would be to start with Test::More and Test::Moose. But also take a look at Test::Class to see if it fits with the way that you want to write tests.

Perl Testing: A Developers Notebook is a great introduction to this topic.

davorg
+5  A: 

In addition to davorg's excellent answer, I'd like to note that I'm still mostly using Test::More (with some help from Test-Differences, Test-WWW-Mechanize-LibXML, and other modules). I can recommend against using Test.pm which is old and stupid, and Test::Simple, which is a small subset of the Test::More functionality.

There are also Test::Most (an extension of Test::More), Test::Class and Test::Class::Most, which some people prefer, but I didn't take the time to learn them yet.

There's an ongoing debate about whether a plan (= tests' count) is a good thing or not. Personally, I've already noticed a case in someone else's CPAN module where the count of tests was different on my system's than on them (and varied based on different versions of DBI (IIRC)) and which convinced me that the plan is a good thing. As a result I've created Test-Count which is a way to count and update the assertions count based on annotations inside well-formed comments (and which supports source code in other languages besides Perl 5). I'm still supporting it, so if you need anything, give me a shout.

Shlomi Fish
+1 for mentioning the need for a plan. I know I'm probably a minority here (heck, Schwern who owns Test::Builder seems to disagree with me, based his prior comments); but I find that accurate test counts are quintessential for any remotely moderately complex test framework, if for no other reason than they serve as a line of defense against test framework's bugs.
DVK
+1  A: 

I'd recommend Test::Class as a basis for your test framework. It makes for better structured, more modular code. And you can still use Test::More and other Test modules with it. Also check Test::Exception.

jira
+1  A: 

Others have suggested Test::Class; I have found the following PDF overview from $foo Magazin (I didn't write it, just found it) was quite helpful for some exampled beyond what the POD documentation provides.

mrisher
A: 

It is not really essential for moose development. However, if you're into Web Development , I think Test::WWW::Selenium is becoming indespensable for testing javascript-heavy web pages and their behavior in the most common web browsers (firefox, iexplorer, googlechrome, etc.)

knb