Assert for DUnit by Tiriss

Use .NET Assert class in DUnit



Version 1.2 of Assert for DUnit

With the first (public) release of Assert for DUnit (V1.1) still a lot of stuff was missing and some bugs were found. In the mean time I added a lot of stuff I needed and also added some stuff I didn't really had a need for myself but just added to make the Assert class complete. This new stuff is now released in Version 1.2!

I also added some new methods (not present in in NUnit) that I needed because of the differences between Delphi and .NET. The most important one is the  Assert.AreEqual(const aExpectedValue, aActualValue; aTypeInfo: PTypeInfo; aMsg: string = ''); method. This assertion allows you to call AreEqual with as an extra argument the typeinfo for a certain type. This enables you to do assertions for enumerations and sets. So for example:

  AlExpected := taLeftJustify;
  AlActual := taRightJustify;
  Assert.AreEqual(AlExpected, AlActual, TypeInfo(TAlignment));

This gives a nice readable error message: "expected: <taLeftJustify> but was: <taRightJustify>". Unfortunately you can't pass the expected values as constants (like taLeftJustify in this example) into the method, you need to assign the expected value to a variable first.

Further this release of Assert for DUnit adds the units tests for the product it self. This project test the correct workings of the Assert methods, but can also be used as a reference on how to use Assert for DUnit!

So to summarize what's new in 1.2

Why Assert for DUnit?

When you (like me) develop in Delphi as well as in C#, then you might know that both have an xUnit dialect. DUnit for Delphi and NUnit (or MSTest) for C#. However both dialect use a different way of testing. Delphi uses for example:


	CheckEquals('Hello', SayHello);

		

Where in C# you use


	Assert.AreEqual("Hello", SayHello());

So if you switch between the C# and Delphi, you always need to remember which syntax to use ("was it CheckEqual or CheckEquals?").

Having enough of that, I thought that it should be possible to use the C# syntax in Delphi (I choose for C# syntax because it is the newer syntax and I expect I will be writing more and more C# and less Delphi in the future). Of course there is a little problem with the buildin Assert() method in Delphi, but it wasn't hard to work around it.

Further using a special Assert class also makes it easier to find the Assert method you need, because the code completion isn't cluttered with all kind of methods that have nothing todo with doing the test. And using this method it is easier to enhance unit tests with new assert classes like for example StringAssert!

What you need to know before you can use it

You can simply start to use it in your own tests by adding the unit DUnitAssert to your uses clause (and making sure that Delphi can find it) and then descend your TestCase classes from the new TAssertTestCase class instead of from the original TTestCase (supplied with DUnit), and off you go.

It's not finished nor complete yet, but feel free to add stuff to it and mail it to me, I will do my best to incorporate it into the code.

The last things you need to know about it are:

Have fun!

 

© MMVIII Tiriss