I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 218, comments - 2281, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

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.

Print | posted on Thursday, March 22, 2007 5:08 PM | Filed Under [ .Net ]

Feedback

Gravatar

# 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?
4/13/2007 11:01 PM | Travis Troup
Gravatar

# 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,
4/14/2007 3:11 PM | Mladen
Gravatar

# 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
9/20/2007 2:22 PM | josiane
Gravatar

# 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...
9/20/2007 2:30 PM | Mladen
Gravatar

# 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!
4/28/2008 7:03 AM | prashant
Gravatar

# 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;amp;quot;1&amp;amp;quot;, &amp;amp;quot;sch&amp;amp;quot;};
ShellApplication app = new ShellApplication(args);
ShellApplication.Start(args);
FormTester frmTester = new FormTester(&amp;amp;quot;ShellForm&amp;amp;quot;);
frmTester.Properties.Text = &amp;amp;quot;UI Testing framework changed this!&amp;amp;quot;;
}
}
}
5/20/2008 12:48 AM | Anonymous
Gravatar

# 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.
5/20/2008 2:05 PM | Mladen
Gravatar

# 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
5/29/2008 12:35 PM | Andi
Gravatar

# 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.
5/29/2008 4:12 PM | Mladen
Gravatar

# 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
5/29/2008 6:02 PM | Andi
Gravatar

# re: NUnitForms and failed DragDrop registration - problem of MTA vs STA

I think it will be going with [STAThread]
5/30/2008 11:38 AM | Andi
Gravatar

# 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.
6/2/2008 10:27 AM | Andi
Gravatar

# re: NUnitForms and failed DragDrop registration - problem of MTA vs STA

that's why you need MTAThread
6/2/2008 11:03 AM | Mladen
Gravatar

# 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
6/2/2008 11:40 AM | Andi
Gravatar

# 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.
6/2/2008 11:49 AM | Mladen
Gravatar

# 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
6/4/2008 11:52 AM | Andi
Gravatar

# re: NUnitForms and failed DragDrop registration - problem of MTA vs STA

no idea. i've never used FitNesse.
6/4/2008 11:54 AM | Mladen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET