views:

329

answers:

4

Hello,

I'm diving into unit testing and I would like to know if it is possible to test some special functionality of a dialog or a form with NUnit. I have tried NUnitForms, but it lacks good documentation and seems that nobody is mantaining that project. Is it true?

If you have any experience testing Windows Forms, please point me in the right way. Any comments or information are appreciated. Thank you!

+2  A: 

Automated GUI testing tends to be very brittle, and I would guess that this is why NUnitForms isn't being maintained (if that is true).

Spare yourself the agony and look at applying design patterns such as Model View Controller or Model View ViewModel. This will allow you to unit test the UI logic in a much more robust manner.

Mark Seemann
Good advice but there are no mainstream MVC libs/templates for WinForms (afaik).
Henk Holterman
Mark Seemann
The logic I want to test in a form is input validation logic... I think that's still reasonable even using the MVC pattern.
yeyeyerman
Yes, but you can still implement input validation logic in an underlying Model instead of directly in your Windows Forms code.
Mark Seemann
Never heard of Model View ViewModel for Winforms (I thought it was introduced with WPF). SCSF (a GUI layer on top of CAB), if somewhat complex, provides a nice Model View Presenter framework, and is quite unit testable.
Sylvestre Equy
+2  A: 

I am having the same problem with NUnitForms. I can launch a form and click a button, but I can't figure out how to get dialogs working, and I can't find much about it at all. I think this blog was the most useful: http://blogs.msdn.com/john_daddamio/archive/2006/11/06/getting-started-with-nunitforms.aspx

I had to remap the NUnitForms assembly in my app.config to get it to work with the latest version like this:

<configuration>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly>
        <assemblyIdentity name="nunit.framework"
          publicKeyToken="96d09a1eb7f44a77"
          culture="neutral" />
        <bindingRedirect oldVersion="2.2.7.0" newVersion="2.5.2.9222" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

( http://duncanjasmith.blogspot.com/2007/08/using-nunitforms-with-nunit-framework.html )

I'm hoping this thread will help me, but I might still try project white ( http://www.codeplex.com/white ).

Colin
The project White looks promising. The thing is that I would like to test GUI logic within my Continuous Integration server, so I don't have to bother running the tests on every revision.
yeyeyerman
Have you seen this:http://stackoverflow.com/questions/532994/ui-testing-framework-continuous-integration
Colin
Thanks for the link!
yeyeyerman
+1  A: 

I've had a lot of success using the UI Automation framework that comes as part of .Net 3.0 and later. I've written a tutorial that might help get you started.

Samuel Jack
A: 

There's a very good and easy to understand example of a MVC framework produced by Ayende, see this entry on his blog for more details. It's a works by adding a very light-weight infastructure to your application that wires up buttons to events automatically for you.

By using this type of framework you are forced to not have any logic in your View, only in your Presenter. Then you can easily unit test the logic without touching WinForms.

I've started a port of Effectus to .NET 2.0 and WinForms, it's here if your interested.

Matt Warren
Hey, your las link goes to a page that says not found! :-O
yeyeyerman
Yeah I think github was broken earlier on, but it should be working now.
Matt Warren