I have written before about running Windows Forms GUI tests on a compiled application exe here.
And i must say that it works GREAT!
I had a few problems of which the most annoying was the Drag and Drop functionality. If you have it enabled with AllowDrop = true
on your controls or any other 3rd party controls the moment your control gets loaded you will get this error:
System.InvalidOperationException: DragDrop registration did not succeed. --->
System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode
before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.
at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
So what to do? At first i disabled all DragDrop in my app so i can start writing test, but this was a hack.
The problem is because my app was running under Single-Threaded Apartment (STA) and NUnit framwork runs under Multithreaded Apartment (MTA)
I asked on the SourceForge NunitForms Help Forums and a fellow named Steve Clark pointed me in the right direction.
NUnit has a configuration section where you can define the apartment model you wish to run it in.
This is a simple config file of the startup project for NUnitForms test running with the STA fix:
<?xml version="1.0" encoding="utf-8"?>
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA" />
<!-- See ThreadPriority enum for other valid values -->
<add key="ThreadPriority" value="Normal" />
Hope this helps.