tags:

views:

140

answers:

8

Hello :)

I'm looking to unit testing as a means of regression testing on a project.

However, my issue is that the project is basically a glorified DIR command -- it performs regular expression tests and MD5 filters on the results, and allows many criteria to be specified, but the entire thing is designed to process input from the system on which it runs.

I'm also a one-man development team, and I question the value of a test for code written by me which is written by me.

Is unit testing worthwhile in this situation? If so, how might such tests be accomplished?

EDIT: MD5 and Regex functions aren't provided by me -- they are provided by the Crypto++ library and Boost, respectively. Therefore I don't gain much by testing them. Most of the code I have simply feeds data into the libraries, and the prints out the results.

+3  A: 

The value of test-after, the way you are asking, can indeed be limited in certain circumstances, but the way to unit test, from the description would be to isolate the regular expression tests and MD5 filters into one section of code, and abstract the feeding of the input so that in production it feeds from the system, and during the unit test, your test class passes in that input.

You then collect a sampling of the different scenarios you intend to support, and feed those in via different unit tests that exercise each scenario.

I think the value of the unit test will come through if you have to change the code to handle new scenarios. You will be confident that the old scenarios don't break as you make changes.

Yishai
The regex and MD5 functions are provided by libraries -- I didn't write these.
Billy ONeal
Also, the main reason I'm looking at this is I'd like to re factor a large hunk of my code.
Billy ONeal
Then you need the unit tests to make sure that the refactoring hasn't broken your code. Then you can enhance, fixing tests that you meant to break because you changed functionality; then you can refactor again with the revised unit tests. Repeat - it is valuable.
Jonathan Leffler
Also, remember how Knuth got into testing (of TEX). He put himself in the ugliest, meanest frame of mind he could, and battered at the code with complex constructs nested inside other complex constructs, creating evil whereever he could. But after the code passed the tests, he was confident enough to claim that he'd removed the last bug from TEX -- it worked as designed.
Jonathan Leffler
@BillyONeal, the fact that you want to refactor a large chunk of code is also a good candidate for testing - you will know that your refactoring didn't change behavior. What does your code do that makes a large hunk worth refactoring? Odds are it remains fundamentally correct to test that given input x, it outputs result y, even if the heavy lifting is done by third party libraries.
Yishai
This is a good answer (+1) ... and I agree some sort of test is worthwile -- but in this case the idea of testing the public interface of the program rather than the individual code cases will likely lead to better results because I can compare my results against built-in OS tools.
Billy ONeal
+1  A: 

Unit testing can still provide value in a one-man show. It gives you confidence in the functionality and correctness (at some level) of the module. But some design considerations may be needed to help make testing more applicable to your code. Modularization makes a big difference, especially if combined with some kind of dependency injection, instead of tight coupling. This allows test versions of collaborators to be used for testing a module in isolation. In your case, a mock file system object could return a predictable set of data, so your filtering and criteria code can be evaluated.

Joe Skora
A: 

When you were writing the code, did you test it as you went? Those tests could be written into an automated script, so that when you have to make a change in 2 months time, you can re-run all those tests to ensure you haven't regressed something.

In my experience the chance of regression increases sharply depending on how much time goes by after the time you finish version 1 and start coding version 2, because you'll typically forget the subtle nuances of how it works under different conditions - your unit tests are a way of encoding those nuances.

Jeffrey Kemp
A: 

An integration test against the filesystem would be worthwhile. Just make sure it does what it needs to do.

Matt Hinze
+1  A: 

The value of regression testing is often not realized until it's automated. Once that's done, things become a lot easier.

That means you have to be able to start from a known position (if you're generating MD5s on files, you have to start with the same files each time). Then get one successful run where you can save the output - that's the baseline.

From that point on, regression testing is simply a push-button job. Start your test, collect the output and compare it to your known baseline (of course, if the output ever changes, you'll need to check it manually, or with another independent script before saving it as the new baseline).

Keep in mind the idea of regression testing is to catch any bugs introduced by new code (i.e., regressing the software). It's not to test the functionality of that new code.

The more you can automate this, the better, even as a one-man development team.

paxdiablo
A: 
  1. Is unit testing valuable in a one-man shop scenario? Absolutely! There's nothing better than being able to refactor your code with absolute confidence you haven't broken anything.

  2. How do I unit test this? Mock the system calls, and then test the rest of your logic.

Esteban Araya
How exactly does one "Mock the system calls"?
Billy ONeal
@Billy: Are you asking what mocking framework to use?
Esteban Araya
I have never heard of a mocking framework.
Billy ONeal
+3  A: 

Is unit testing worthwhile in this situation?

Not necessarily: especially for a one-man team I think it may be sufficient to have automated testing of something larger than a "unit" ... further details at "Should one test internal implementation, or only test public behaviour?"

ChrisW
Accepted this answer because it gave me a new way of looking at the testing -- test the output of the program itself rather than trying to test individual functions inside.
Billy ONeal
A: 

I question the value of a test for code written by me which is written by me

Well, that's true now but in a year it will be you, the one-year-more-experienced developer developing against software written by you-now, the less experienced and knowledgeable developer (by comparison). Won't you want the code written by that less experienced guy (you a year ago) to be properly tested so you can make changes with confidence that nothing has broken?

Chris Latta