views:

28

answers:

2

The caption may sound a little weird. Allow me to elaborate using an example.

The Object.Equals function usually requires that a.Equals(a) returns true. Unless you're doing something twisted in your code, every class should adhere to this rule.

So we could write a generic unittest that checks all available classes to comply. (We might exclude classes explicitly marked by some attribute.)

And instead of just using Equals, we're also checking for correct behavior of all implementations of, say, IComparer and whatever standard interface you can imagine.

Now, my question is: Does this exist already? If not, why would it be a bad idea?

+1  A: 

I'm not aware of the existence of such a thing, but it sounds like a fine idea.

This is on the border of a convetion-based test and static code analysis - you could just as well imagine such a rule written as a custom Code Analysis rule, but it's probably easier to write it as a unit test.

In our latest project, we have some convention-based tests that simply loop through all types in a given assembly and verifies that it conforms to some convention that we have. One could definitely imagine doing the same with e.g. the Equals method.

Mark Seemann
+1  A: 

I can think of one reason for this feature missing: It is required to create an instance of every class to be tested. Now, some classes do not provide a default constructor. Possible solution:

  1. We only check classes that do have a default ctor. Seems ok though, better than nothing.

  2. We may implement a factory for all non-default-constructable classes. Not sure about the best way to do that, but I assume there is a feasible solution.

Obviously, we can exclude all classes provided by the framework to reduce the amount of work and problems.

mafutrct
Well, you could always use AutoFixture to create instances of classes... http://autofixture.codeplex.com/
Mark Seemann
Yea, I guess something like that would work.
mafutrct
+1 BTW. Good answer :)
Mark Seemann