Why you should think about TOOP- Testable Object Oriented Programming

Update: Mark has a point - Using "TOOP" and "TOOD" are catchier acronyms - I've changed them accordingly.

I think it's time that Object Oriented Design\Programming got a little face lift.

We live in interesting times, where quality and the quest for testability and continuous integration are starting to sprout all over the development world.

It's in these times that one needs to think about one simple fact - in many ways, pure object oriented design does not go well hand in hand with the notion of testable design. I wrote about a classic example of this in this post - FxCop is removing Testability features in favor of object oriented pureness.

here are some examples for this dissonance in which OOP\OOD usually says "hide" while Testable design guidelines dictate "Expose":

  • OOD:  Do not expose any private members or methods that are not needed by the developer using your API .
  • Testable: Find a way to expose or replace private instances of objects using interfaces, injection patterns, public setters and more.
  • OOD: Seal classes to inheritance by default unless you explicitly mean for others to extend them
  • Testable: Don't seal by default (unless it's a security risk) so that others can reuse your code and override what they need in their tests.
  • OOD: only make methods virtual explicitly (in java methods are virtual by default which is an interesting strategy which I very much like) when you want others to override them.
  • Testable: when possible, make virtual the default so that inheritors can override them to break any dependencies found in their tests.
  • OOD: Use singletons to make sure only one instance exists. do not allow anyone to touch that private instance.
  • Testable: Allow others to replace Singleton instances to break any dependencies in their tests.
  • OOD: make types private or internal by default unless you want to expose them specifically.
  • Testable: Think about and expose types that you know would help the testability of your API
  • OOD: Don't add unnecessary APIs
  • Testable: Add special APIs like methods, interfaces and types especially for testability purposes. These are necessary.

What if we added the word "Testability" into the OOD paradigm?

"Testable Object Oriented Design: TOOD"

"Testable Object Oriented Programming: TOOP"

This paradigm would take into account the testability factor of each design, and will allow for specific design issues to be solved with testability as the winner. In many ways you can't have a truly testable design without the proper (and heavy) use of some of the object oriented principles (polymorphism is as ubiquitous in testable designs as Falafel in Israel - everywhere you look there's another corner offering it). Combining the values of both paradigms will lead to a better and more solid future for software quality for all of us.

But where do we start?

Published Sunday, February 25, 2007 5:36 PM by RoyOsherove

Comments

Sunday, February 25, 2007 9:36 PM by Kent Boogaart

# re: Why you should think about OOTP - Object Oriented Testable Programming

I'm a firm believer that unit testing should be a platform service, not an add-on via libraries. If the platform supported a unit test mode, we wouldn't have the conundrums that we do.

Imagine, if you will, a 'unit test' switch on the CLR such that:

- only unit tests can be executed

- unit tests have unrestricted access to their unit under test.

- unit tests have unrestricted ability to mock types that they wish to.

- perhaps, even, mocks could be created automatically based on some unit test attributes.

What I'm getting at is that until unit testing is a platform service we are going to have hacks around accessibility, mockability and all round testability.

Love to hear your thoughts Roy,

Kent

Monday, February 26, 2007 2:27 AM by Michael Dorfman

# re: Why you should think about OOTP - Object Oriented Testable Programming

Where should we start?  Well, I'd start by bringing your post to the attention of Brad Abrams and Krzystof Cwalina, since they seem to be the most influential guys in the Microsoft community in terms of API design. If you can get them engaging with your ideas, you're halfway there.

Personally, I like my polymorphism with hummus.

Monday, February 26, 2007 3:02 AM by Yoni

# re: Why you should think about OOTP - Object Oriented Testable Programming

I think OOP and Tests go well together because OOP is about hiding implementation details and so are tests. If tests are to allow constant, painless, refactoring they must not interfere with the internal representation of classes in the system but rather test their behavior using the same public interface the rest of the system uses. If you find a need to test internal aspects of a class then that class should probably be split into two or more classes which can be tested seperatly and combined at runtime to produce the desired behavior.

Monday, February 26, 2007 1:43 PM by John Bledsoe

# re: Why you should think about OOTP - Object Oriented Testable Programming

Yoni, I'm not sure I can agree that tests are about hiding implementation details.  I'm relatively new to unit testing, but my understanding is that a unit test should verify the function of a single member, which may often include affecting non-public fields/properties/etc.  Even in the scenario where you split "larger" classes so that each can be black-box tested, you still need to expose those classes publically so that a testing framework can manipulate them.

And on falafel, I spent two weeks in Israel in the 90's and ate falafel from a street vendor almost every day.  Since coming home, I have yet to find the same quality falafel anywhere, (in Ohio at least.)

Monday, February 26, 2007 3:35 PM by Eli Lopian

# re: Why you should think about OOTP - Object Oriented Testable Programming

I wouldn't want to change my design just for the sake of testability (see http://www.elilopian.com/2007/02/26/object-oriented-testable-designed-you-must-be-out-of-your-mind/)

Tuesday, February 27, 2007 11:03 AM by Yoni

# re: Why you should think about OOTP - Object Oriented Testable Programming

John, I think it depends on what constitutes a unit in your design. For me the smallest cohesive  unit is usually a class, a single member isn't worth testing out of context because it isn't a complete behavior (and if it is it's probably the only public member of the class to begin with).

Wednesday, February 28, 2007 4:47 PM by mawi

# re: Why you should think about OOTP - Object Oriented Testable Programming

Thanks for this! How quickly we get that debate again! (plug warning) I am doing a workshop on testability and encapsulation and this inspires me.  I almost thought that everyone already had gone past the encapsulation debate and that I wouldn't get anyone to discuss with - but here it is again!

workshop: http://www.spaconference.org/spa2007/sessions/session67.html

Tuesday, March 06, 2007 6:41 AM by Nat

# re: Why you should think about TOOP- Testable Object Oriented Programming

I think some of the things you list as "OOD" are not object oriented design principles at all and, in fact, violate object oriented design principles.

* Singletons are global variables accessed through a global name.  Strict OO design would only allow objects to access other objects that are referenced through instance variables or method parameters.

* Only make methods virtual explicitly?  Polymorphism is fundamental to OO design.  Without polymorphic methods you can't do OO design!

# Eli Lopian’s Blog (TypeMock) » Blog Archive » Object Oriented Testable Designed - You must be out of your mind

Pingback from  Eli Lopian’s Blog (TypeMock)  » Blog Archive   » Object Oriented Testable Designed - You must be out of your mind

Wednesday, June 20, 2007 4:20 PM by ISerializable - Roy Osherove's Blog

# HTTPSimulator - Simulating HTTP Requests for unit testing made easier

Phill Haack just released HTTPSimulator - a class to help with running tests against a simulated Http

# HTTPSimulator - Simulating HTTP Requests for unit testing made easier « Tuff Stuff

Pingback from  HTTPSimulator - Simulating HTTP Requests for unit testing made easier « Tuff Stuff