Running NUnit from the console

27.10.2009 22:33Comments
So I have already posted about the source structure for our projects, how to move the common libraries to an external link with SVN and building our projects with Nant. In this post I will show you how to run unit tests in our environment from the command line. First of all if you are not familiar with Nunit, it is a Unit Testing framework for .NET. It's an excellent framework, it's free and you can download it from here. One of the good things about NUnit, is that it doesn't require to be installed in order to be used. You can copy the folder and execute it from wherever you want. This makes it ideal for placing NUnit in our lib folder as an external link. One thing to have in mind is to include the version of NUnit in the folder name to avoid problems in the future. So your folder structure should look something like this: nant folder As I have pointed in the post about basic structure for our source code, I showed a very common practice when it comes to unit tests projects which is to name the project after the project being tested. So if I have a library project name "MyLibrary" I will usually have a unit test project like "MyLibrary.Tests". Once we build our "Tests" project, we are ready to run the tests from the command line. Making the actual call is very simple, you just execute from the command line the nunit-console application with the path to the tests (the *Tests.dll file) and optionally /xml parameter for specifying a place for the output xml file. In order to make this available to the whole team, we are going to build a simple batch file. We are going to put this file in the trunk folder with the following content: .libnunit 2.4.8nunit-console.exe .srcMyLibrary.TestsbindebugMyLibrary.Tests.dll We can add a "build-result" folder inside our trunk and output the xml file by adding the following parameter /xml=.build-resultMyLibrary.Tests.xml So now the batch file will look like this: .libnunit 2.4.8nunit-console.exe /xml=.build-resultMyLibrary.Tests.xml .srcMyLibrary.TestsbindebugMyLibrary.Tests.dll And finally you can use your favorite xsl in order to visualize the results. There's a very simple xsl file in the NUnit folder called "Summary.xslt" which you can use by editing the xml to add the reference like this: <?xml version="1.0" encoding="utf-8" standalone="no"?> <!--This file represents the results of running a test suite--> <?xml-stylesheet type="text/xsl" href="summary.xslt"?> Adding this file to the build-result folder is a good idea too. So we have successfully run our NUnit tests of our project, and left a batch file available to the whole team to run the tests in their local machine. Simple as it is, this will come in very handy when trying to calculate the Unit Tests Coverage of your tests.

comments powered by Disqus