Should production components of other developers be used in your test?

Should the production components written by other developers be incorporated into your unit tests?  It depends.  If the component used by your class is not available yet, then there is no choice other than writing a stub component that contains a simple implementation of the objects and methods required by your software according to the interface as you understand it.  A stubbed component should be used for the following cases:

  • The component being used requires use of specialized hardware that you don’t have access to on your computer.
  • The component being used takes a very long time to run.  For example, if it takes much more than 5 minutes, then you should probably be using a stub method that can execute in under a second.  This one is a judgement call.  To some, even 5 minutes might be too long, especially if there are many components that take 5+ minutes.  For others, 10 minutes might be acceptable.
  • The component being used has not been written yet.

If there are methods of a component that typically take a long time to execute or require special hardware that other developers will not have access to on their computer, then the author of that component should provide a substitute test stub class with methods that execute quickly so that the interface can at least be tested.  The reason for this is so that users of that component can test the interface easily and quickly.  The reason why it needs to be quick is that the faster it executes, the more tests can be written and time saved.  It is simply more efficient.

If the author of a component has provided a test stub, then that should be used over a stub written by the user of that component.  Of course if the production component is available then that should be used instead of a stub if it’s methods do not take a long time to execute and does not require interfacing to specialized hardware.

Prev          Next          Back to write the unit tests

Copyright 2011 by Robert G. Bryan

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s