The online community for software testing & quality assurance professionals
 
 
Calendar   Today's Topics
Sponsors:




Lost Password?

Home
BetaSoft
Blogs
Jobs
Training
News
Links
Downloads



Software Testing >> Unit Testing

Pages: 1
jvisprime
Newbie


Reged: 11/22/11
Posts: 3
How to unit test a memento object?
      #691831 - 11/22/11 04:52 PM

Hi everybody,

I am developing the unit tests for a module that is a kind of memento object (see design patterns for details). The module is implemented in C and has two API functions: Save() and Restore. The Save() function receives a list of pointers to the objects whose states it should save. The Restore() function should restore the states of the objects, saved by the Save() function. The module uses linked list library as a storage for the states.

The question is: what is the right way to unit test the module's API functions?

One way is to have a unit test that will do the following:
1. Build a list of objects
2. Call Save() function to save the states of those objects
3. Modify states of the objects
4. Call Restore() function to restore states of the objects
5. Verify that the states of the objects are the same as before they were modified

This will definitely verify the intended modules behavior, but is it a good unit test? The problem that I have with this method is that the unit test calls both API functions and their functionality is not tested in isolation.

The other way is to have a separate unit test for each API function. Stubs can be used to replace the list library that the module uses as a storage.

The unit test for the Save() function will do the following:
1. Build a list of objects
2. Call Save() function to save the states of those objects
3. Verify the content of the stub storage

The unit test for the Restore() function will do the following:
1. Initialize the content of the stub storage, so that contains "saved" states
2. Set the objects to the states, different from the "saved" states
3. Call Restore() function to restore states of the objects
4. Verify that the states of the objects are the same as "saved" sates

This will also verify the modules behavior, but is this a good unit test? The problem that I have with this method is that it relies on the knowledge of the implementation details, namely on the number of state entries in the list and their order (and on the format of the entries for that matter), which may vary depending on the algorithm implemented by the module. Such a dependency makes unit tests, implemented that way, very fragile, i.e. unit tests implementation will have to change with any change in the implementation that alters data saved in the storage.

So From your experience, what is the best way to test such a module or may be to change the design of the module, so it will be more testable?

Thank you,

Jvis


Post Extras: Print Post   Remind Me!   Notify Moderator  
dlai
Junior Member


Reged: 05/02/06
Posts: 1041
Loc: CA, USA
Re: How to unit test a memento object? [Re: jvisprime]
      #691906 - 11/23/11 07:23 AM

Generally a unit test that relies on the implementation is bad. That is usually an indication of tight coupling. Which means that most likely you'll need to inject an interface and a factory creation pattern somewhere in the class. You might want to use a factory pattern for creating the internal linked list of the object, which then can be switched out with a stub factory when you do the unit test.

Code:

class ClassUnderTest() {
protected ILinkedListCreator linkedListFactory;

public ClassUnderTest() {
linkedListFactory = new RealLinkedListFactory();
}

public void save(blah blah blah) {
linkedListFactory.create(blah blah blah)
}

}


class ClassUnderTest_With_TestHarness extends ClassUnderTest {
// override constructor to replace the factory with our test stub.
ClassUnderTest_With_TestHarness() {
linkedListFactory = new LinkedListStubFactory();
}
}




By using interfaces instead of concrete implmentation, you can easily stub or mock out pieces of the class under test. Using the factory creation pattern, you can switch out the internals of the class and insert stubs in their place without adding a dependency on the codes implementation.

--------------------
David Lai
Sr. QA / Test Lead
LinkedIn profile

Edited by dlai (11/23/11 07:34 AM)


Post Extras: Print Post   Remind Me!   Notify Moderator  
jvisprime
Newbie


Reged: 11/22/11
Posts: 3
Re: How to unit test a memento object? [Re: dlai]
      #691912 - 11/23/11 08:42 AM


David,

Thank you for your reply. The problem is not the tight coupling of the module with the linked list, which I can easily stub. The problem is with the content of the linked list, i.e. the data that will be stored in the linked list. This data depends on the implementation of the module, so if the implemented algorithm changes (e.g. entries will be stored in reverse order) then the unit tests should also change. And that is the problem with the second approach.


Post Extras: Print Post   Remind Me!   Notify Moderator  
dlai
Junior Member


Reged: 05/02/06
Posts: 1041
Loc: CA, USA
Re: How to unit test a memento object? [Re: jvisprime]
      #691914 - 11/23/11 09:39 AM

I wouldn't worry about using a real object to test the class under test. The stub or mock is just to help create the exact conditions.

You may want to add like a equals(object) method, for comparing the linked list with itself. That way you can move the comparison of the contents into the class that stores the contents, so it can be updated when that particular class gets updated.

--------------------
David Lai
Sr. QA / Test Lead
LinkedIn profile


Post Extras: Print Post   Remind Me!   Notify Moderator  
jvisprime
Newbie


Reged: 11/22/11
Posts: 3
Re: How to unit test a memento object? [Re: dlai]
      #691918 - 11/23/11 11:26 AM

The mechanics of the process is not the issue. The issue is where from do I get the list, populated with the data for the Restore()?

Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 3 anonymous users are browsing this forum.

Moderator:  AJ, Jeanj 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 3191

Rate this topic

Jump to

Contact Us | Privacy statement SQAForums

Powered by UBB.threads™ 6.5.5