Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Another Entlib example: Separate integration tests from unit tests (and learn to know the difference) | Main | Enterprise library -- Test usability and "runnability" »
Monday
Apr112005

Test maintainability: consider creating a test fixture "per method" instead of "per class"

If you find yourself writing lots of tests for a single method on your tested class - consider making a specific fixture for just one method or feature. It would make the class more maintainable and logically sound. 
For example, a complex Login class might have one "IsLoginOK" method that has dozens of unit test for various logical angles and features.
 
You might have a fixture that looks like this:
 
[TestFixture]
public class LoginManagerTests_IsLoginOK
{
   [Test]
   public void ReturnsFalseOnNonExistingUser()
   {
   ...
   }
 
}
 
note that:
  • The class name suggests the main feature or function being tested by postfixing it to the name
  • The is no need to prefix methods in the class with the method name since it is clearly the only method being tests. Each test case is a variation of feature of this method.
  • You might end up with multiple class test fixtures for one tested class. That's OK, just remember to put them under a specific folder in your test project with the name of the tested class like this
    • ProjectName
      • LoginManagerTests
        • LoginManagerTests_IsLoginOK
        • LoginManagerTests_ChangePassword
        • LoginManagerTests_General
  • You would probably end up with a class that tests various "small" features which were left over from other fixtures. name it to [TestedClass_General] or [TestedClass_Misc] so that it's testing purpose is understood. 

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>