Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: TDD of non-module code

by Voronich (Hermit)
on Jan 25, 2012 at 23:05 UTC ( #949990=note: print w/replies, xml ) Need Help??

in reply to TDD of non-module code

I'm not sure if it's at all comforting, but you're running in to one of the consistent early hurdles of TDD as it relates to componentized (hush, it is too a word) architecture. TDD nearly demands you expose your code to the tests. I've been an aspiring TDDer for about five years now and still run into this problem like a skier hitting a patch of mud. I've finally convinced myself that the increased exposure pays off more in testability than the protectionism of hiding your deep implementation does in safety.

THAT said I like the command-line switch idea. What I most frequently do is push everything into a method (including an actual "main" method.) And I keep a "test" method as well. It's not particularly pretty, but then I'm certainly the only one who runs tests on my code.

At least if you had a testable mode selectable off the command line you'd be able to include your tests in a larger scale test environment.


Replies are listed 'Best First'.
Re^2: TDD of non-module code
by davies (Parson) on Feb 10, 2012 at 09:45 UTC

    Thanks, all, for the help. I've gone down the command line switch route and have got my code working. But the code has two areas that smell to me, so I come again pleading for help. :-)

    I have tests in three files that are called from a fourth, which is as follows:

    use strict; use warnings; use Test::Harness; runtests(["podext.t"], ["excelpod.bat"], ["ExcelPOD.t"]);

    This works fine, but I don't like the business of having to run the .bat file. However, I can't find any way of putting a command such as -t with command line parameters into the list of test files to be run without getting an error. The second smell is in, some of which is:

    use if (1 == scalar @ARGV && "-t" eq $ARGV[0]), "Test::More"; ... if (0 < scalar @ARGV && '-t' eq $ARGV[0]) { #Tests no warnings; eval {"plan tests => 63;"}; use warnings;

    If I write this without all the conditionals, everything works, but running in "live mode" (as opposed to test mode) I get a message saying that no test were run. This might be mistaken for an error message and cause confusion, so I want to suppress it. But writing the "use if" line means that running in live mode generates a compile time error when I give the test plan (and I can find no way of providing that in the "use if" command). Wrapping it in an "eval" command results in a warning of a useless use of a constant in void context, and the only way I can find to avoid that is to suppress warnings.

    As I say, the code is working and the tests are running, but I'd be a happier bunny either if I knew how to avoid the kludges or if I knew that they were unavoidable.


    John Davies

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949990]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2017-01-18 02:58 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (160 votes). Check out past polls.