Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

You've gotta have tests!

by talexb (Canon)
on Aug 23, 2018 at 05:11 UTC ( #1220894=perlmeditation: print w/replies, xml ) Need Help??

I've been building an ecosystem of scripts for a client over the last few months, and recently I needed to make use of some functionality I'd left in place in one of my modules. Before starting development, I checked to see how the module tests were laid out, in order to give me more insight into how the module works, since I wrote it a few months ago.

Then I discovered there were no tests.

Narrator: A vague sense of unease flitted through the developer's mind, but he resolutely shrugged it off.

OK, no problem -- I'd just write the tests, as it was a fairly simple module.

Narrator: But it wasn't as simple as he imagined it to be.

After writing the tests, I came to the realization that the way I'd written the module meant it wasn't as extensible as I originally thought. And tests might have revealed that shortcoming.

So I spent some development time more clearly defining what I wanted to module to do, how it should be called, and made changes in one of the scripts that use the module. And found some more issues.

Narrator: Several cups of coffee later ..

The good news is that I now have a module that works the way I need it to, complete with tests that show that it works correctly. All of the scripts that use this module have been updated. And tomorrow I can start work on the feature that uses this improvement.

But the moral of the story remains: It doesn't matter how simple a module is. As soon as you decided you'd put some logic into a module -- that's the point that you should have also written the tests that prove that the module's logic is correct. There's not no excuse for skipping that part.

You've gotta have tests!

Update: Minor edit: not -> no in second to last paragraph.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: You've gotta have tests!
by Your Mother (Bishop) on Aug 23, 2018 at 05:51 UTC

    ++. The thing about tests is they feel foreign at first and like a huge hit. I think it’s probably three times harder to write the tests than the code; ballpark, back of the napkin. So a one month project might be a four month project if all testing, fixtures, mocking, and build niceties are met. The thing about that is that it cuts debug and future related development and refactoring and customer upset time—and maybe training time since reading tests is a perfect way to learn a code base—down geometrically. So a two day bug is a two hour bug. It won’t make those three months up immediately but after a year… it’s probably paying interest and dividends.

    And tests eventually become fun to write. At this point, I enjoy writing tests almost as much as greenfield code and much more than dealing with legacy code.

        At this point, I enjoy writing tests almost as much as greenfield code and much more than dealing with legacy code.

      Right .. and I find tests give you a much more nuanced understanding of what the module does, and really clarifies your thinking. I believe legacy code with tests would be a lot less scary than what you usually see, code without tests.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: You've gotta have tests! (with coverage)
by toolic (Bishop) on Aug 23, 2018 at 12:45 UTC
    ... and run coverage (Devel::Cover) to prove the tests hit what you expect them to hit.
Re: You've gotta have tests!
by eyepopslikeamosquito (Chancellor) on Aug 23, 2018 at 23:11 UTC
Re: You've gotta have tests!
by TheloniusMonk (Sexton) on Aug 23, 2018 at 12:01 UTC
    Narrator: This has nothing to do with Gollum.
    Yes it does!
    Who said that?
    Wasn't me!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1220894]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-09-22 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (190 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!