Thread: Unit Testing Abstract Class

  1. #1
    Registered User
    Join Date
    Mar 2012

    Question Unit Testing Abstract Class


    I have an abstract class and I need to do unit Testing on it... The problem is I can't create object of that class to test constructor and some methods.

    For the purpose of the unit testing do I need to make it not Abstract and when I'm done with my testing it goes back to an abstract Class?


  2. #2
    Registered User
    Join Date
    Jun 2005
    No. If you have to modify a class before and after unit testing, you defeat the purpose of unit testing by allowing false passes (e.g. an error may be negated by modifying the class, so it passes the test, and then the error is re-applied after testing).

    The more usual approach is to do unit testing (on instances of) all the concrete derived classes that takes account of all requirements supported by the base class.

    Sometimes, it may be helpful to create a minimal concrete class for the specific purpose of unit testing (so, if that concrete pass passes unit testing, you deem the abstract class has passed as well). That requires some documented analysis to support the claim of "if this derived class passes unit testing so does the base class". It also potentially allows you to limit the scope of unit testing on other derived classes to concerns specific to those derived classes. The problem with this approach is that the "minimal concrete class for unit testing" should probably not be used in production code, and may conflict with other system requirements (e.g. absence of dead code).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2012, 01:11 PM
  2. C and C++ unit testing framework
    By Siddu_Kyocera in forum C Programming
    Replies: 1
    Last Post: 05-21-2010, 08:34 AM
  3. what can not be tested in C++ unit testing?
    By Alexpo in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2010, 12:31 AM
  4. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  5. Unit testing tools for .NET ?
    By gicio in forum C# Programming
    Replies: 0
    Last Post: 11-10-2003, 04:43 AM

Tags for this Thread