NUnitForms and failed DragDrop registration - problem of MTA vs STA
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"?> <configuration> <configSections> <sectionGroup name="NUnit"> <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> </configSections> <NUnit> <TestRunner> <!-- Valid values are STA,MTA. Others ignored. --> <add key="ApartmentState" value="STA" /> <!-- See ThreadPriority enum for other valid values --> <add key="ThreadPriority" value="Normal" /> </TestRunner> </NUnit> </configuration>
Hope this helps.
Legacy Comments
Travis Troup
2007-04-13 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA I'm trying to implement this fix, and it's not working for me. I have an exe that I'm trying to test and a dll that has my nunit tests in it. I'm using TestRunner with NUnit. I've created a config file for the app exe and the unit test dll, but am still getting the drag/drop registration exception. I'm even messing around now with changing the configs for the nunit and testrunner dll's. Any ideas? What am I missing? |
Mladen
2007-04-14 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA how are you trying to test the exe? you have to put the configuration file of you exe and the dragdrop fix in the same NUnit runner project's Config section, |
josiane
2007-09-20 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA hi i am testing a C# application using NUnit, and i am getting the same exception: dragDrop registration did not succeed... i changed the configuration files in a way that tests run under STA, but im still getting the same error. please reply me if im missing something |
Mladen
2007-09-20 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA are you testing with NUnit or NUnitForms? no idea what your problem might be... but you probably missed something in the config files... |
prashant
2008-04-28 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA Thanks a lot ! It worked for me.You gave me exactly what I needed.Cheers! |
Anonymous
2008-05-20 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA Hi, I am new to NUnitForms and NUnit in general. I got a simple test to run. I am using CAB and Casle Windsor (http://www.castleproject.org/). When I try to do a drag-drop it crashes. I have a class library C# project that has the tests in it. Here are my questions: 1. Where is the configuration file supposed to be located (in the same directory of the class library? the same directory of the ShellApplication executable?) 2. What is the name of the configuration file (app.config?) 3. Has anyone tried this framework with CAB or Castle? they both do some nice reflection magic, and I am trying to get the NUnitForms to work with it. Here is my test code that runs the app: using System.Windows.Forms; using Company.CustomApp.CoreShell; using NUnit.Extensions.Forms; using NUnit.Framework; namespace AcceptanceTests { [TestFixture] public class Class1 : NUnitFormTest { [Test] public void FormMainTest() { string[] args = {&amp;quot;1&amp;quot;, &amp;quot;sch&amp;quot;}; ShellApplication app = new ShellApplication(args); ShellApplication.Start(args); FormTester frmTester = new FormTester(&amp;quot;ShellForm&amp;quot;); frmTester.Properties.Text = &amp;quot;UI Testing framework changed this!&amp;quot;; } } } |
Mladen
2008-05-20 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA 1. the config has to be the config for your nunit app 2. it's app.config 3. not me. no idea on others. |
Andi
2008-05-29 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA Hallo; I do not want to use the NUnit GUI or Runner, so I do it this way: class myTestRunner { static void Main(string[] args) { GUITester Tester = new GUITester(); // the class that normally would have [TestFixture] Tester.InitializeTests(); // the Method that normally would have [SetUp] Tester.Test01(); // the Test that normally would have [Test] Tester.Test02(); Tester.DeInitialize(); // the Method that normally would have [TearDown] } } how should the Configuration file "App.config" look, make it work? Thaks all Andi |
Mladen
2008-05-29 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA what do you use then? your configs should be in the project that you're running this in. |
Andi
2008-05-29 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA I use only the reflection-methods from nunit, for GUI testing and I have another runner (fitnesse and another's) This was an example (myTestRunner) to know how should the Configuration file "App.config" look. thanks |
Andi
2008-05-30 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA I think it will be going with [STAThread] |
Andi
2008-06-02 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA It has almost worked with STAThread, but i have problem with the MTAThread by TearDown, class myTestRunner { [STAThread] static void Main(string[] args) { GUITester Tester = new GUITester(); // the class that normally would have [TestFixture] Tester.init(); // the Method that normally would have [SetUp] Tester.Test01(); // the Test that normally would have [Test] Tester.Test02(); Tester.Verify(); // the Method that normally would have [TearDown] } } Bug:Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. |
Mladen
2008-06-02 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA that's why you need MTAThread |
Andi
2008-06-02 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA What should i do, the Application that i test need STAThread! this has not worked: http://64.233.183.104/search?q=cache:2zjJSZVW7uwJ:www.codeproject.com/KB/COM/opcdotnet.aspx%3Ffid%3D2198%26df%3D90%26mpp%3D25%26noise%3D3%26sort%3DPosition%26view%3DQuick%26select%3D2541074%26fr%3D1+STAThread+mtathread+Cross-thread+operation+not+valid&hl=de&ct=clnk&cd=1&gl=de |
Mladen
2008-06-02 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA if your app needs STA then you can't test it like this. i don't know of a good way to fix this. |
Andi
2008-06-04 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA how should the Configuration file "App.config" for "FitNesse" look? FitNesse starts NUnit and my application. thanks |
Mladen
2008-06-04 |
re: NUnitForms and failed DragDrop registration - problem of MTA vs STA no idea. i've never used FitNesse. |