Posts

Showing posts with the label testing

vim productive note

When writing tests for Perl objects (or modules), I like to store the name of the module in a named buffer/register in vim. I usually visually highlight the object(or module) name, then put it in the "n" buffer. Yank into n: "ny Paste out of n: "np I guess the m or n buffer might make sense. If you forget like I do what buffer your using you can dump them in vim with: registers So it was...

SOAP::Lite and Test::MockModule

Intro As part of a project to convert an existing SOAP client library over to a new version of calls, I found that I needed a way to test potential faults and new data formats without requiring live calls. After I built it for the new stuff, I used it to test existing calls and errors as well. This code was running live for several years before I needed to change it for the new version, this inspired me to be very careful and invest in testing. On a side note, I found plenty of bugs just creating the test suite in the existing code that had been there for years. Creating the Test Suite To create the test suite, I needed to pretend to get SOAP server responses. I turned to Test::MockModule to step in and provide hooks to return them. I found that mocking these responses were pretty straightforward once I figured out how to use the SOAP::Lite deserializer. (like most things, its easy once you know how) The following outlines what I did to create the suite, well at least the process of u...

A new must use module - Test::Exception

I write quite a few unit tests that have methods or subs that throw exceptions (like most good code should... this rule has numerous good exceptions! hahaha, sorry couldn't resist.). A few months ago I ran across Test::Exception . In the past I would often write unit tests for these cases like: { my $err; my $obj; eval { $obj = MyObject->new; }; ## constructor expected to fail $err = $@; ok $err, "constructor - missing parameters"; ## or for methods that aren't suppose to throw eval { $obj = MyObject->new( server => 'localhost' ) }; ## check constructor $err = $@; ok !$err, "constructor"; isa_ok $obj, 'MyObject'; } This construct became very tiring, verbose and distracts from what is really going on. (Yuck!) With Test::Exception, I can convert these annoying eval blocks into nicely contain lines. Example from above: use Test::Exception; { my $obj; throws_ok { $obj = MyObject->new; } qr/Error Message to match/, 'constructor...