dotnet thoughts 

a dotnet developer's technical blog

How to integrate Nunit with Visual Studio 2008

While working with my tech lead, I saw he was using Nunit with VS2008. He was running tests same time he was also debugging the code. When I discussed the same with my colleague, he told me to how to integrate Nunit with Visual Studio 2008. Today I found another way to do this; but it wasn’t support debugging, but it wasn’t not depend on the project, instead it will act like a option in Visual Studio.

  1. The first way is starting the NUnit as an external program, and passing the assembly as the command line argument to NUnit. This can be achieved by Project Properties > selecting the Debug Tab > Select the “Start an external program” option from the Start Action section; instead of the Start project (default) option. And select the NUnit executable. (In my machine it is C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit.exe). Next enter the assembly file name in the Command line arguments textbox in the Start Options section. That’s it. We can run unit tests as well as we can debug the code, while running the unit tests.
  2. The second option is using External Tools option in Visual Studio. Select Tools > External Tools > Add

    Title: NUnit
    Command: (path to nunit.exe)
    Arguments: $(BinDir)/$(TargetName).dll
    Initial directory: $(ProjectDir)

    Click Apply, then OK. Then it will display as a menu item in the Tools menu. This will open the current project in NUnit.

We achieve the same using various plugins like ReSharper and Test Driven .Net.

Happy UnitTesting :)

Some best practices for C# developers

Here is some best practices for C# developers.

  1. Check the C# source files with StyleCop – (http://code.msdn.microsoft.com/sourceanalysis) – StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project.
  2. Check the assemblies with FxCop – (http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx) – FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements.
  3. Unit Test applications with NUnit – (http://nunit.org) – NUnit is a unit-testing framework for all .Net languages. For testing ASP.Net pages there an extension of NUnit is available it is called NUnitAsp – (http://nunitasp.sourceforge.net/) , but the problem in NUnitAsp is this project is officially dead. And for unit testing Windows forms White (http://white.codeplex.com) and NUnit.
  4. Use CLR Profiler -(http://www.microsoft.com/downloads/details.aspx?familyid=FD02C7D6-5306-41F2-A1BE-B7DCB74C9C0B&displaylang=en) – The CLR Profiler allows developers to see the allocation profile of their managed applications.
  5. Use latest .Net framework features, like LINQ, Extention methods, Partial methods, Simple Properties etc.( http://www.microsoft.com/net/)

Unit Testing Windows Forms using White and nUnit

Few days back I attended a session on TDD with ASP.NET MVC 1.0 on K-MUG Cochin User group meeting, from Shiju Varghese, the session was excellent one. Currently I was working on a Windows application, so after the session I searched for tool, using that I can Unit test my windows application. After few searches, I got one link from Codeplex, called White. White is open source project from Thoughtworks, you can download white from Codeplex. To unit test the Windows applications; you need both White as well as NUnit. Also I got another tool called NUnitForms, but I think this project is almost dead :( I tried it with VS 2008, in a simple VC# application.
TDD often called Red-Green approach, which means you will initially write code for getting the Red signal. After getting the red signal, you will write some code to get the green signal. After getting the Green signal, your need to refactor the code.

HelloWorld Application (TDD Approach)

Requirements
Hello world application: Need a UI, which contains a Button with Text “HelloWorld”, clicking on that button, it will pop up a MessageBox with title “Helloworld”.

Unit TestCases.

  1. Check the UI exists or not
  2. Check the UI contains a Button
  3. Click on the Button, will it showing a MessageBox with title “Helloworld”?
  4. Development

  1. Created a Windows Application Project, and added a Class library project to the solution.
  2. Added References of White.NUnit.dll, White.Core.dll and nunit.framework.dll to the class library.
  3. Wrote the code for setup and Teardown in the class library
  4. [TestFixture]
    public class HelloworldTest1
    {
    	private string applicationPath = @"C:\Helloworld\HelloWorld.exe";	//Path of the Windows app to test
    Application helloworldApp;
     [SetUp]
    public void SetupTest()
    {
    //Launching the application.
    this.helloworldApp = Application.Launch(applicationPath);
    }
    [TearDown]
    public void ShutdownTest()
    {
    this.helloworldApp.Kill();//Stopping the application.
    }
    }
    

Now we can add Test cases.
Test Cases

  1. Checking the UI existance
  2. [Test]
    public void CheckUI()
    {
    Window helloworldWin = this.helloworldApp.GetWindow("UI");
    Assert.IsNotNull(helloworldWin);
    }
    

    This testcase will fail, because I don’t have a Form with the title UI, so nUnit will fail, with the reason “Couldn’t find window with title UI in process 980, after waiting for 5000 ms”. Now you got the green signal. Now change the Title of the form to UI, build the solution, run the NUnit test case. It will return a Green signal. You passed the first test case.

  3. Check the UI contains a Button or not
  4. [Test]
     public void CheckButtonExists()
    {
    Window helloworldWin = this.helloworldApp.GetWindow("UI");
    Button button = helloworldWin.Get<button>("button1");
    Assert.IsNotNull(button);
    }
    

    This will also fail because we haven’t added a Button on the Form. Lets add a button with Text “Helloworld” and compile and re-run the application. It will give you a green signal.

  5. Click on the Button, will it showing a MessageBox with title “Helloworld”
  6. [Test]
    public void CheckMessageBoxDisplaying()
    {
    Window helloworldWin = this.helloworldApp.GetWindow("UI");
    Button button = helloworldWin.Get</button><button>("button1");
    button.Click();
    Window messageBox = helloworldWin.MessageBox("Helloworld");
    Assert.IsNotNull(button);
    messageBox.Close();
    }
    

    It will fail, because there is nothing in the click event, so NUnit will display an Object reference error, like this “HelloWorldTest.HelloworldTest1.CheckMessageBoxDisplaying: System.NullReferenceException : Object reference not set to an instance of an object.”
    Lets add some code in the click event

    private void button1_Click(object sender, EventArgs e)
    {
    MessageBox.Show("this is click event","hello");
    }
    

    It will again fail, because we are looking for a MessageBox window with Title HelloWorld. But in the actual code, we wrote Title as “hello”. Now change the Title to “Helloworld” and re-run the application. It will run successfully.

Hope you got some idea about unit testing windows applications using White. Let me know your comments and feedback if you are facing any issues. You can also test WPF applications with White, but I didn’t tried it.

You can download

  1. NUnit from NUnit website
  2. White from Codeplex
    « Newer Posts