For quite awhile now I've read/believed that meta-programming unit tests is an anti-pattern. After all, tests are supposed to be DAMP not DRY right? So converting:
it('successfully does x', function() {
assert(foo.x());
});
it('successfully does y' // ...
it('successfully does z',// ...
to:
var types = ['x', 'y', 'z'];
for (var i = 0; i < types.length; i++) {
var type = types[i];
it('successfully does ' + type, function() {
assert(foo[type]());
});
}
should be a bad idea ... right?
The thing is, I recently had a junior programmer write some unit tests in this meta-programming way, and I can't explain why those tests are problematic.
Normally I'd explain that it's hard to tell what exactly is failing in meta-programmed tests ... but in this programmer's tests its perfectly obvious because they modify the test label every time (ie. it says "successfully did x", so you know exactly what broke).
I'd also explain that it makes the test code itself messy ... but it's hard to argue that a simple foo[type]
statement is more messy than repeating the entire contents of the test multiple times.
So, my question is two-part:
- Is meta-programming tests always a bad practice?
- If not, what criteria determine whether meta-programming a test is ok? Is it just "it's fine as long as the test code and output is clear", or are the other criteria to consider?
P.S. Please understand that I'm asking for an answer based on established unit testing best practices (which I believe is a legitimate SO question), NOT for subjective opinions (which would not be legitimate).