Home > Programming > Is Test::Class the standard yet?

Is Test::Class the standard yet?

Recently, I’ve been coming across a few Perl projects (both at work, and in the wild) that are reasonably large and complex, have test suites, but don’t use Test::Class. And I really struggle to understand why. Most of them have decent test suites, at least in terms of coverage. But, personally, I have a hard time looking at the test files and understanding exactly what’s going on.

Most other widely used languages have at least one class-based testing framework that is the de-facto standard, usually based around xUnit. I find there are several benefits to using a framework like Test::Class:

  1. As I mentioned, it makes your tests more readable by breaking them up into methods (I blogged a while ago about breaking them up even further for maximum readability). This ensures you’re clear about what you’re testing in every test, and you don’t get a really complex setup, used by a whole lot of different tests. Not only is that difficult to read, but it quickly becomes complex to maintain, and the tests become fragile, throwing you false negatives.
  2. Makes code-reuse easier, with inheritance (ok, so we all use roles now, but inheritance is still better than nothing).
  3. Makes it easy to setup fixtures by storing them in $self, using setup, teardown, etc. methods, all of which encourages more code-reuse
  4. Helps with test driven development, by allowing you two map one application class to one test class, or set of classes where necessary. You can do it with a bunch of .t scripts as well, I guess, but I usually find it easier with classes.
  5. And many more…!

We like to have a lot of options in Perl, and I’m fine with that, but you usually should have a good reason for deviating from the norm. So is Test::Class the norm (or did I just get a bad sample)? If it’s not the standard, why isn’t it? It seems to have a pretty thin set of dependencies, is stable and mature code, and fits right in with Test::Builder and friends.

In my opinion, you should use Test::Class unless you have a damn good reason not to!

About these ads
Categories: Programming Tags: , ,
  1. April 20, 2010 at 2:30 pm

    I also like Test::Class however for a lot of simple testing situations its overkill. Actually its worse since Test::Class lets you do so much it can very easily make understanding test cases ever more. You end up needing tests for the tests :)

    I also find Test::Class doesn’t play very well with Moose (or it didn’t) and thats important to me. There’s a few other Unit Test style systems like Test::Class now, so you might want to poke around CPAN

    • Mutant
      April 20, 2010 at 9:18 pm

      If you need tests for the tests, you’re doing something very wrong. And I don’t think it’s anything to do with Test::Class. It’s important to keep tests as simple as possible, and to be clear about exactly what each test is trying to do. Of course it helps if you write testable code.

      Only the simplest test suites won’t benefit from a class-based approach, in my opinion. Just as only the simplest applications won’t benefit from OO, or some other way of organising your code well. (I’m not completely tied to Test::Class, or even a class-based approach. I just think it’s important to write your tests well, and organise them properly).

  2. April 20, 2010 at 2:56 pm

    There are a couple of problems at work here. First, there wasn’t a great tutorial for it (though I have a link to one I wrote for Modern Perl Books).

    Second, many people don’t seem to think of test classes as being objects. I often see z_teardown methods designed to run after a subclass’s teardown method instead of having a teardown method call its SUPER:: version. They don’t think about state encapsulation or how different test methods need to start with a pristine state.

    I’m going to have to buckle down and put all of this together and write a much better tutorial for this module, but it’s going to require some more features added to this module to handle a few extra things we’re needing.

    • Mutant
      April 20, 2010 at 9:21 pm

      That may be so, but I think there’s easily enough there, including docs and tutorials, that there’s no excuse for not using it.

  3. April 22, 2010 at 6:24 am

    You asked for why people don’t do it; you got at least two really good answers, and then dismissed them as not good enough. Do you really want to know why or are you just not great at reading Test::More tests?

    • Mutant
      April 22, 2010 at 7:30 am

      I dismissed them because I think they’re easy arguments to dismiss. The point of my post was that Test::Class is good enough to use now, and class-based testing (or some superiour way of organising tests than simple scripts) is standard across modern languages, but the Perl community doesn’t seem to want to know. I don’t think it’s to do with Test::Class, but more like a lack of will to change.

      Ovid was apologetic for a module he works on.. but we’re all the biggest critic’s of our own work, and I think Test::Class is easily good enough.

      John’s point that sometimes Test::Class is overkill is correct (and I admit my reply was possibly worded poorly), but I still maintain that the point that Test::Class (or equivalent ) becomes necessary is only just beyond the most trivial projects.

      I am honestly baffled as to why the Perl community – normally quick to pick up on solid industry standards – has been so slow to adopt class-based testing, when every other language seems to do it, and has been for years.

  4. Meepmeepmeep
    April 26, 2010 at 11:12 am

    Maybe it’s simply because the cpan page for it is flat out bad marketing. It begins with “testing just like in that other language you likely aren’t even using or even dislike. how exciting!”, proceeds to atrocities like “shift->{test_array}” in a very dense synopsis, which also reintroduces useless things like “count your tests!”, then reintroduces this xUnit thing without even explaining WHAT it is, but feels it necessary to expound on the “where from?” and the “why?”.

    Rewrite it so that someone who has no idea about what xUnit is can instantly grasp what Test::Class does and you might start getting people interested in actually looking at it. So far it’s very likely though that people don’t actively decide against it after having had an in-depth look, but just can’t be arsed to even try and find out what it is.

  1. April 21, 2010 at 12:31 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: