ISerialized .Net, C#, Scrum and agile software development

23Mar/100

Running NUnit in an STA thread

While unit testing a WPF Caliburn application, I bumped into the following error:

The calling thread must be STA

and I ended up with a cross-thread exception. When diving into this problem I found that TestDriver.Net actually runs in STA (acronym for Singel Thread Apartment) by default, but  NUnit is not! It is however a fairly simple configuration change to get NUnit running in STA.  Add or update your app.config in your test project with following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  <sectionGroup name="NUnit">
    <section type="System.Configuration.NameValueSectionHandler"
             name="TestRunner"></section>
  </sectionGroup>
  </configSections>
  <NUnit>
    <TestRunner>
      <add value="STA" key="ApartmentState"></add>
    </TestRunner>
  </NUnit>
</configuration>

As this is located in the test projects app.config file, your NUnit tests will now run in STA.

About Paul Eie

My name is Pål Eie (English alias: Paul Eie) I'm an IT consultant working in Stavanger, Norway. My experience spans from AIX to Windows and from embedded C to Uniface, Java, C++ and C#. My blog will involve everything that is related to technology. As long as it might be of interest to either newbies or seniors, I will blog about it! If you find some of my posts about basic old technology, it just means you know more than many of my other readers!
Blogglisten