Test Windows Forms logic with NUnit
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 ha开发者_Python百科ve any experience testing Windows Forms, please point me in the right way. Any comments or information are appreciated. Thank you!
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 ).
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.
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.
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.
精彩评论