Tip Details Add a Related Tip

Rate as: Positive Negative

Is your unit test really a unit test

Lots of developers and software companies make use of unit tests. Either to just ensure that new code does not break existing code, or because they are using test driven design. One potential problem with unit tests is that as time goes on, the testing suite can become so bloated that developers stop running the unit tests against their code. The test still get run on some sort of regular, or occasional basis, but the tests don't get run every time a developer checks in code (as they should).

One reason that this happens is that often when people are writing unit tests, they aren't. Instead they are writing functional test. While functional tests are absolutely needed and just as useful as unit tests, they should be kept separate so that the "real" unit tests can be run quickly.

The problem that most people make is that unit tests are really intended to test just one "unit" of code. They are not intended to test interactions between objects or different layers in the code base. Those are functional tests. Again, functional tests are absolutely needed. You just want to make sure you separate them so that unit tests can be run quickly.

When you are writing your unit tests, here are some questions you can ask yourself to make sure you are writing a unit test and not a functional test. If you answer "yes" to any of these, you are not in fact writing a unit test.

1) Does the test require communication with the database or any other outside resource like a network? External resources like the database should be mocked out since you are trying to test the thing that talks to the database; the process of communication is functional.

2) Does the test touch the file system? Again, this is an outside resource, it should be mocked.

3) Can test not be run when other test are running? Unit tests should be free of state and should be able to run anytime independent of other tests that are being run.

4) Does the environment, or configuration files, need to be altered to run the test?
Rating: no ratings, 0 total Votes
Categories: programming Programing debugging unit test
Added: on Feb 29, 2008 at 11:06 am
Added By: hindy

Comments on this Tip

Add a Comment
There are no comments for this tip
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Reverse sort order in Bugzilla

100% positive, 1 comment - last added on Jun 13, 2008 at 8:43 am
– Tip added by rlansky on Sep 10, 2007 at 4:26 pm

debugging pages for the iPhone

100% positive, 1 comment - last added on May 11, 2008 at 8:31 pm
– Tip added by an anonymous user on Jul 09, 2007 at 8:24 am

Easy way to create .htaccess files

100% positive, 0 comments
– Tip added by an anonymous user on Apr 01, 2008 at 7:57 am

How to Find a Random Date in PHP

40% positive, 0 comments
– Tip added by Walkere on Feb 07, 2008 at 8:25 pm

Javascript Error: unterminated regular expression literal

100% positive, 0 comments
– Tip added by marty on Jun 26, 2008 at 1:20 pm

Minimize round trips to the server for best performance

100% positive, 0 comments
– Tip added by an anonymous user on Jun 18, 2008 at 4:07 pm

compressing javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jun 12, 2008 at 3:02 pm

Telling when an iframe is done loading

100% positive, 0 comments
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

Our best moment was the sunset...

50% positive, 1 comment - last added on Apr 02, 2008 at 5:56 pm
– Tip added by Billy Cenea on Apr 02, 2008 at 5:49 pm

Opening missing files in JEdit

100% positive, 0 comments
– Tip added by an anonymous user on Mar 24, 2008 at 3:18 pm

Copyright date on sites

Categories: web programming php Smarty
100% positive, 0 comments
– Tip added by an anonymous user on Mar 11, 2008 at 8:14 am

Viewing plist files in Leopard

100% positive, 0 comments
– Tip added by an anonymous user on Mar 06, 2008 at 3:21 pm

How to Syndicate an RSS Feed On Your Site

100% positive, 0 comments
– Tip added by Walkere on Feb 09, 2008 at 5:48 pm

How to Send E-Mail in PHP: A “Share This” Form

50% positive, 0 comments
– Tip added by Walkere on Feb 06, 2008 at 6:45 pm

How to Create a Random Password in PHP

100% positive, 0 comments
– Tip added by Walkere on Feb 03, 2008 at 9:14 am

1067 errors in Actionscript

no ratings, 0 comments
– Tip added by an anonymous user on Jun 17, 2008 at 9:28 pm

Referencing methods in PHP usort

Categories: php coding programming
no ratings, 0 comments
– Tip added by an anonymous user on Jun 13, 2008 at 11:18 am

CakePHP: Working with Associations

no ratings, 0 comments
– Tip added by an anonymous user on May 08, 2008 at 4:09 am

Creating your own Quick Look Plug-Ins

no ratings, 0 comments
– Tip added by an anonymous user on Apr 29, 2008 at 7:34 am

Javascript: How to Create a Random Number

0% positive, 0 comments
– Tip added by Walkere on Feb 23, 2008 at 9:49 pm