views:

219

answers:

1

I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my Unittests also. In order to use it, it should run in STA mode, but since the NUnit Testfixture does not have a main method, I don't know where/how to annotate it...

Thanks!

+5  A: 

Add an app.config file to the project containing your unit tests and include the following:

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

You can verify that the apartment threading is STA with the following C' code:

if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
   throw new ThreadStateException("The current threads apartment state is not STA");
}
Bernhard Hofmann
Simple, yet frustrating, question, immediate working answer : some days I just love SO.
Peter
Almost forgetting : Thanks man
Peter
If you're ever looking for it again (and SO is down) this is from the sample config provided in WatiN. Glad I could help. :)
Bernhard Hofmann
Surprisingly, it even works when using Resharper to run the tests inside VS2010. Many thanks!
Lee Oades