Unit Testing – Calling Test Methods Within Other Test Methods

drytestingunit testing

To test a method that returns a clone of the object it is called on, i need to re-run the test suite on the newly created object if i am to ensure that full functionality is retained, i found my instinct was to clone the object then call all other test methods in sequence i order to reduce code duplication. However that approach now smells to me and i can't pinpoint why. Is there a better way to test this kind of functionality?

Best Answer

Such testing might be cleaner with code contracts. The test methods you are talking about should be invariants (must hold true before and after all public method calls) on your object, rather than unit tests. Your clone function would have preconditions and postconditions (the postcondition would require that the object be equal to the object it cloned, and possibly that its reference was not equal).

In some cases, your code could be statically tested for compliance with such invariants.