Technology with opinion

Wednesday, July 27, 2005

Run NUnit Tests on Compilation

Test driven development is an approach to programming where you write your unit testing code to test for the functionality & requirements first. Then code is written to write the unit of code (class, etc).

For .Net programmers, the tool of choice for unit testing is NUnit. For more information about NUnit, see toolkit below.

I recommend creating a project for your unit testing, .UnitTesting will suffice. To automate the process of testing, it is helpful to have unit testing performed upon compilation, thus every time your code changes you can rerun your unit tests and not have to manually rerun NUnit.

To do this, you need to run NUnit's commandline utility, located in NUnit's Program Files folder \bin.

Create your testing class and methods:

[TestFixture]
Class1
{
[Test]
public void Test1()
{
Project1.MyUnit1 minute = new Project1.MyUnit1();
Assert.AreEqual(myUnit.Add(1,2), 3); // assert an exception if 1 + 2 <> 3
}
}

Next, go to the project properties for your NUnit project, click Build Events on the left-hand side. In the Post-build Event Command Line field paste the following:

"%programfiles%\NUnit 2.2\bin\nunit-console" "$(SolutionDir)\Project1.nunit" > "$(SolutionDir)\Project1.nunit.log"

This assumes your .nunit file is in the root of your solution. Variables above might need to be changed to work with your project. Once you build this logic into your solutions, unit testing can become seamless.

Tuesday, July 12, 2005

XSLT Date/Time & Upper Case functions in .Net Framework

Due to Microsoft's lack of support for XPath 2.0, as well as their dropped support for most of the own extended fuctions, all that remain accessible now is customizable script support in XSLT, for those who need functionality outside of the scope of XSLT 1.0.

To perform many useful functions such as upper case, date/time formatting, etc, you must write custom script. Below is an example of date/time formatting I'm performing within my XSLT document for a BizTalk project, although this will work for any .Net application.

First: Add the following namespaces to your XSLT declaration

xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://www.tempuri.org/User">


Second: Write Custom functions

<msxsl:script implements-prefix="user" language="C#">
<![CDATA[
public string FormatDate(XPathNodeIterator node, string xpathExpr, string format)
{
XPathNavigator nav = node.Current;
if(nav.MoveToAttribute(xpathExpr, ""))
{
DateTime dt = DateTime.Parse(nav.Value);
return dt.ToString(format);
}
else
{
throw new ApplicationException("Cannot transform document, specified XPath expression is not valid.");
}
}

public string ToUpper(XPathNodeIterator node, string xpathExpr)
{
XPathNavigator nav = node.Current;
if(nav.MoveToAttribute(xpathExpr, ""))
{
string fullName = nav.Value;
return fullName.ToUpper();
}
else
{
throw new ApplicationException("Cannot transform document, specified XPath expression is not valid.");
}
}
]]>
</msxsl:script>


Finally: Call your custom XSLT function, inline within your XSLT


<xsl:value-of select="user:FormatDate(., 'Value', 'M/d/yyyy h:mm:ss tt')"/>
<xsl:value-of select="user:ToUpper(., 'Value')"/>