Thursday, April 29, 2004

MbUnit now features several new assertions that touch different aspects of the applications. They provide new tool to easily add complicated assertions into your unit tests. All the new asssertions usually come under two version: the positive and negative "Not" version.

Includsion: Between and In (Assert class)

Two new assertions in Assert: Between and In which behaves as their SQL cousins:

int i=0;
Assert.Between(i, 1, 10);

int[] list = new int{1,2,3};
Assert.In(0, list);

String: StringAssert class

String is a very special type that totally deserves it's own assertions. Using Regular expression (System.Text.RegularExpression.Regex class), we can assert for complicated patterns. For example, checking for SQL injection in a string.

StringAssert.IsNotEmpty("");       // asserts that the string is not empty,
StringAssert.Like("hello", @"\w"); // asserts if it matches a regular expression
StringAssert.FullMatch("12-2344-21",@"\d{2}-\d{4}-\d{2}");// asserts that the match is full

Security: SecuAssert class

Performs some basic security based assertion: check that user is in role, check authentication, etc...

SecuAssert.WindowsIsInAdmin(); // asserts that is in administrator role
SecuAssert.IsAuthenticated(); // asserts that is authenticated
...

Reflection: RefleAssert class

Asserts that types can be assigned to each other, are instance of, or contains desired members:

Type parent;
Type child;

RefleAssert.IsAssignableFrom(parent, child); // asserts that parent is assignable from child
RefleAssert.HasMethod(typeof(String), "Substring", int, int);// asserts that System.String posseses Substring(int, int) method

Performance: PerfAssert class

Provides timing assertions. In the future it might also include memory related assertions:

CountDownTimer cdt = PerfAssert.Duration(10); // asserting that the duration to cdt.Close will last less that 10secs
...
cdt.Stop(); // throws if lastes more that 10 secs.

Data: DataAssert class

Data assertions compare DataSet, DataTable, DataRow and DataColumns content and schema:

DataSet ds;
DataSet ds2;

DataAssert.AreSchemaEqual(ds,ds2); // asserts that schemas of ds, ds2 are equal
DataAssert.AreDataEqual(ds,ds2);   // asserts that data in ds,ds2 are equal
DataAssert.AreEqual(ds,ds2);       // asserts that data and schema of ds,ds2 are equal

DataTable t,t2;
DataAssert(t,t2);

XML assertions: XmlAssert class

Xml comparaison is entirely done by the XmlUnit project from http://xmlunit.sourceforge.net

Formating error message

Assertion that accepts an error message support a "String.Format" like syntax: you can pass a format string and arguments:

string name="Marc";
Assert.AreEqual("John",Marc,"The name {0} does not match {1}", "John",name);

All suggestions for other assertions are welcome...

posted on Friday, April 30, 2004 5:17:00 AM UTC  #    Comments [9]
Tracked by:
"replica coach purse" (online) [Trackback]
Monday, June 06, 2005 6:10:02 PM UTC
Peli's Blog
Monday, June 06, 2005 6:10:02 PM UTC
overflow
Monday, June 06, 2005 6:10:03 PM UTC
Peli's Blog
Monday, June 06, 2005 6:10:04 PM UTC
Assert.AreEqual("John",Marc,"The name {0} does not match {1}",name, "John");
<br>
<br>seems like it should be
<br>
<br>Assert.AreEqual(&quot;John&quot;,name,&quot;The name {0} does not match {1}&quot;,name, &quot;John&quot;);
<br>
Max Guernsey, III
Monday, June 06, 2005 6:10:05 PM UTC
Thanks, updated.
Jonathan de Halleux
Monday, June 06, 2005 6:10:05 PM UTC
Please change SecuAssert =&gt; SecurityAssert
<br>...
<br>You can depreacte the old one.
<br>
<br>RefleAssert is hideous !!!
<br>
<br>Ricky
<br>
Ricky Datta
Monday, June 06, 2005 6:10:07 PM UTC
Indeed, I have changed those in the code.
Jonathan de Halleux
Monday, June 06, 2005 6:10:07 PM UTC
Scooter's Musings
Monday, June 06, 2005 6:10:08 PM UTC
Scooter's Musings
Comments are closed.