Using WatiN and NUnit

First you need to create a config file for your test assembly. The naming of this file depends on how you use NUnit (more info here):

  • If you load a test assembly directly, such as mytests.dll, the config file must be named mytests.dll.config located in the same directory mytests.dll is in.
  • If you load an NUnit project, such as mytests.nunit, the config file must be named mytests.config.
  • If you use NUnit’s Visual Studio support to load a Visual Studio project or solution, such as mytests.csproj or mytests.sln, the config file must be named mytests.config.

Next, paste the following XML into the config. The trick is in the <add key=”ApartmentState” value=”STA” /> part. nunit-gui and nunit-console will now run all your tests in the assembly on a single threaded apartment Thread.

<?xml version="1.0" encoding="utf-8" ?>
   <sectionGroup name="NUnit">
    <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
    <!-- Valid values are STA,MTA. Others ignored. -->
    <add key="ApartmentState" value="STA" />