views:

104

answers:

4

I have two different classes that share a common interface. Although the functionality is the same they work very differently internally. So naturally I want to test them both.

The best example I can come up with; I serialize something to a file, one class serialize it to plaintext, the other to xml. The data (should) look the same before and after the serialization regardless of method used.

What is the best approach to test both classes the same way? The tests only differs in the way that they instantiate different classes. I dont want to copy the entire test, rename it and change one line.

The tests are currently in JUnit, but Im going to port them to NUnit anyway so code doesnt really matter. Im more looking for a design pattern to apply to this test suite.

+4  A: 

Create a common abstract base test class for the test.

abstract class BaseTest{

 @Test
 public void featureX(){
    Type t = createInstance();
    // do something with t
 }

 abstract Type createInstance();
}

ConcreteTest extends BaseTest{

    Type createInstace(){
        return //instantiate concrete type here.
    }
}
Thomas Jung
Good option, but you'll also need to mark `BaseTest` ignored, in order for xUnit not to try to run it.
Victor Sergienko
Depends on the runner. There are more or less intelligent. The former see that the class is abstract and the later can be configured to ignore the class depending on patterns.
Thomas Jung
+1  A: 

I'd reuse the code either with inheritance or aggregation.

To have the shortest code, I'd move a tested instance creation to a factory method in, say, XmlImplementationTest class, and inherit a TextImplementationTest from it:

XmlImplementationTest extends TestCase
{
  Interface tested = null
  Interface createTested() { return new XmlImplementation() }
  ...
  void setUp() { tested = createTested(); }
}

TextImplementationTest extends XmlImplementationTest
{
  override Interface createTested() { return new TextImplementation() }
}

This is not completely correct OO design, as it's TextImplementationTest is NOT a XmlImplementationTest. But usually you don't need to care about it.

Or readdress the test method calls to some common utility class. This would involve more code and not show proper test class in test reports, but might be easier to debug.

Victor Sergienko
A: 

In C#, I'd use a generic helper method to test both cases, something like:

internal static void SerializationTestHelper<T>() where T : IMySerialize
{
    T serialize = new T();
    // do some testing
}

[TestMethod]
public void XmlTest()
{
    SerializationTestHelper<XmlSerialize>();
}

[TestMethod]
public void PlainTextTest()
{
    SerializationTestHelper<PlainTextSerialize>();
}
Andy C
+1  A: 

I tend to avoid any relations between test classes. I like to keep testcases (or classes) as atomic as possible. The benefit of using inheritance here doesn't outweight the strong coupling you get by it.

I guess it would be helpful, if you could share the validation of the result of the two classes (Assuming blackbox tests). If both classes are able to let you set an outputstream, you might validate that, while the classes itself write to PrintWriter or FileWriter (or whatever you need in your cases).

Furthermore I would avoid to create files during unit-tests, because it might take too much time (+ it might not work on the build machine) and therefore delay your build.

pimpf0r
Very valid points indeed, but keeping them totally separate would mean something like 1000 lines of (near) identical code. Changing the interface, or how it works, seems a bit like a can of worms when two tests should be updated. But maybe youre right, maybe thats something you get when you write two classes with to similar logic.
mizipzor
And you wrote the classes *before* you wrote the testMaybe that teaches you a lesson. ;-----)
pimpf0r