Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: What is the best way to add tests to existing code?

by adrianh (Chancellor)
on May 22, 2004 at 14:23 UTC ( #355583=note: print w/replies, xml ) Need Help??

in reply to What is the best way to add tests to existing code?

Please let me know if I've missed important ideas or strategies and which of the points below you consider to be the most important.

I think you're list pretty much has it covered, and I completely agree with chromatic's point about writing tests for new functionality.

The other strategy that I'd add is making sure that you write tests around any code that you're going to improve.

If you've got some gnarly code that you want to refactor write some tests to exercise the behaviour that you want to preserve before you change the code.

In particular, I'm interested to know the best way to add tests to existing code.

The advice I would give is to remember that tests exist to help improve the code. They're not an end in themselves.

A counter productive practice that I've seen is to go through a large piece of legacy code and add developer tests for everything. Doing this with legacy code not driven by tests will produce a test suite that is brittle in the face of change. When you get to the refactoring you're going to find that you're going to be continually throwing away a lot of the new tests so you don't get any benefit from them.

In my experience it's much more effective to build the test suite around the changes you make to the code. Add tests when you add new features. Add tests around code that you're refactoring. Add tests to demonstrate bugs. In my experience just following those three rules naturallys build a test suite around the most important code.

  • Comment on Re: What is the best way to add tests to existing code?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://355583]
[ambrus]: Corion: some of these stupid syntax highlighters assume that too. just look at the table in http://perldoc. functions/pack. html for example.
[haukex]: ..."yet" ;-) I haven't had to deal with Dist::Zilla yet but I've heard about how it's a big setup
[ambrus]: I really don't like automagic stuff. I'm happy when computers do exactly what I tell them, even if that means they sometimes do the wrong thing.
[ambrus]: And I don't much like syntax highlighters. If you need a syntax highlighter to understand your code, then your code is written unclear.
[ambrus]: And if you need a syntax highlighter to color parenthesis green and numbers black and letters blue, then you're using the wrong font.
[ambrus]: I have to tolerate syntax highlighters when other people use them, but I don't use them myself. And sorry for the rant.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2017-02-27 12:45 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.