Tests invoked with ApartmentState MTA?

640 views
Skip to first unread message

Gulli

unread,
May 10, 2007, 3:09:30 PM5/10/07
to TestDriven.NET Users
Hi,

I'm trying out WatiN and it works fine when I run my test method from
a Main method marked with [STAThread].

When I run my test method using TestDriven.Net 2.6.2096 beta, I get
this:

failed: The CurrentThread needs to have it's ApartmentState set to
ApartmentState.STA to be able to automate Internet Explorer.
System.Threading.ThreadStateException
Message: The CurrentThread needs to have it's ApartmentState set to
ApartmentState.STA to be able to automate Internet Explorer.
Source: WatiN.Core
StackTrace:
at WatiN.Core.IE.CheckThreadApartmentStateIsSTA()
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, LogonDialogHandler
logonDialogHandler)
at WatiN.Core.IE..ctor(String url)

and calling SetApartmentState in the beginning of the test (or in
TestFixtureSetup) fails because the apartment state has already been
set to MTA.

I get the same behavior in TestDriven 2.5.2708 beta, and the same when
I test in debugger, or with coverage, or whatever.

Googling around, I seem to see Jamie and the WatiN website and others
in agreement that TestDriven applies STA by default. Anyone know why
it might behave differently? Am I doing something dumb?

The test code is really trivial:

[TestFixture]
public class WatinTest
{
[TestFixtureSetUp]
public void Setup()
{
if (Thread.CurrentThread.GetApartmentState() ==
ApartmentState.Unknown)
{

Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
}
else if (Thread.CurrentThread.GetApartmentState() ==
ApartmentState.MTA)
{
throw new InvalidOperationException("apartment state has
been set to MTA");
}
}

[Test]
public void BasicWatinTest()
{
using (IE ie = new IE("http://localhost/testuri"))
{
ie.Button(Find.ByValue("submit")).Click();
}
}
}

Regards,

- Gulli

Gulli

unread,
May 10, 2007, 3:12:27 PM5/10/07
to TestDriven.NET Users
Oops, and I should note that I am running TestDriven.Net inside Visual
Studio Team System 2005 with SP1 applied, and that the tests are
written against MbUnit.

(Excuse the noise!)

Regards,

- Gulli

Jamie Cansdale

unread,
May 10, 2007, 3:49:44 PM5/10/07
to TestDri...@googlegroups.com
Hi Gulli,

When executing tests using TestDriven.NET the ApartmentState should
only set to MTA if your App.config file contains the following:

<configuration>
<appSettings>
<add key="ApartmentState" value="MTA" />
</appSettings>
</configuration>

Otherwise it should default to STA. Could you try running the
following test and let me know what it outputs:

[Test]
public void ThreadTest()
{
Console.WriteLine(Thread.CurrentThread.ApartmentState);
}

Thanks, Jamie.

--
http://www.testdriven.net
http://weblogs.asp.net/nunitaddin

Gunnlaugur Thor Briem

unread,
May 11, 2007, 6:28:28 AM5/11/07
to TestDri...@googlegroups.com
That test outputs MTA. The ApartmentState appsetting did not occur anywhere.

I figured this out; MbUnit overrides the ApartmentState, setting it to MTA by default (presumably by forking a new thread to run the tests). And the resolution is:

[TestFixture(ApartmentState = ApartmentState.STA)]

and it even says so in the fine manual. Sorry I didn't check there before asking for your time!

Thanks,

    - Gulli

Jamie Cansdale

unread,
May 11, 2007, 6:56:10 AM5/11/07
to TestDri...@googlegroups.com
> [TestFixture(ApartmentState = ApartmentState.STA)]
>
> and it even says so in the fine manual. Sorry I didn't check there before
> asking for your time!
>
No problem. The solution is now here for other people to find. :-)

Cheers,
Jamie.

Hawthorn

unread,
Jun 7, 2007, 7:34:05 PM6/7/07
to TestDriven.NET Users
This is an issue with WatiN and NUnit and only concerns TestDriven.Net
insofar as it shells you out to NUnit.

The WatiN documentation addresses this.
http://watin.sourceforge.net/apartmentstateinfo.html

We fixed this by adding the following into our app.config for each
project that uses NUNit and WatiN

<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" />
</TestRunner>
</NUnit>

Reply all
Reply to author
Forward
0 new messages