Yet ANOTHER post about testing private methods, but this one I hope will be a little different to the norm.
People are always wondering whether a private method should in fact be public or the functionality extracted into another class where it can be tested, or end up making other compromises.
Many programming/scripting languages off a Reflections class, so with that in mind, why can't we automate a way of making our private methods testable? For example, let's say we have a class with a private method that we want to test, surely something like this will work:
class ClassWeWantToTest {
private somePrivateMethod([args, ...]) {
// Do stuff.
}
}
class ClassWeWantToTest_TestWrapper extends ClassWeWantToTest {
public somePrivateMethod_test([args, ...])
{
return this->somePrivateMethod([args, ...]);
}
}
Such testing layers can be made manually and automatically in languages that allow it. There could even be a third party tool that understands the syntax, which will parse a class and generate a layer. Obviously, private methods will only be made public to the tests. In normal use, the private methods remain private.
Why hasn't it been done already? Is this a really stupid idea? I assume it is because it hasn't been done already. It would certainly help with class-clutter, where classes are just being created to help with testability. I know, it would expose the entire class, but so what? The developer knows how it works and can now test more aspects of his code without having to work blind. The developer would be able to work with the class with or without the wrapper which would give even more flexibility.
Aucun commentaire:
Enregistrer un commentaire