views:

349

answers:

4

I love to Extend my Assert.AreEqual to many different classes, the known one is the CollectionAssert of course, but I can think of some more such as: ImageAssert, XmlAssert etc..

Did you Create your own Assert classes? and what kind of new would you like to create?

A: 

A lot of my tests revolve around loading a object that (like a CuttingPath) with a known good state.. Performing the test, then comparing the result to the loaded object. If they differ then somehthing 'happened' to cause a change in the code.

This approach save tons of time and allow for custom comparisons when needed.

RS Conley
+2  A: 

I like the feel of the Assert class, but wanted something that would serve more as a general validation framework. I started with Roger Alsing's article on using extension methods, and now have a system that works like:

Enforce.That(variable).IsNotNull();
Enforce.That(variable).IsInRange(10, 20);
Enforce.That(variable).IsTypeOf(typeof(System.String));
etc.

If any enforcement fails, it throws an exception. I have been considering refactoring so that I can also incorporate a non-critical evaluation that does not throw an exception. Some like Check.That as a variant of Enforce.That which would return booleans, but have extension methods with identical signatures.

What I like about this approach, so far, is that I can use these in my unit tests, as well as for pre-validation and post-validation concerns in my actual code without referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. I put it in my root assembly for my application framework, and Enforce is at the root, so it is very easy to get to.

joseph.ferris
A: 

I think that if you're refactoring your tests to reduce duplication then you end up creating your own framework as a by-product, and of course your test framework will have assert helpers that make sense in your context.

An example I have in mind was when testing xhtml report we ended up with tests that looked something like:

  assertCoverageEquals(45.5);

where behind assert coverage was something like:

  assertPercentage(COVERAGE_ID, 45.5);

and then behind that was something that used xpath to get the value and another method that knew what the formatting was for percentages.

Jeffrey Fredrick
A: 

I've just added an implementation to ImageAssert as I wrote above (in my question) I would be glad to hear more of that kind of samples

thanks.

rabashani