62

I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my unit tests 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.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Peter
  • 47,963
  • 46
  • 132
  • 181
  • By curiosity, will it not suffice to add the attribute [STATThread] on top of your TextFixture/TestMethod/TestClass? – Will Marcouiller Mar 12 '10 at 16:29
  • It only works on Methods, and it didn't work on fixturesetups, testmethods,... Ofcourse I might have overlooked sth. You are welcome to answer if you find any other solution. – Peter Mar 12 '10 at 16:37

4 Answers4

105

If you are using nunit 2.5+, you can use the new The RequiresSTAAttribute at class

[TestFixture, RequiresSTA]

or assembly level.

[assembly:RequiresSTA]

No need for config file. check: http://www.nunit.org/index.php?p=requiresSTA&r=2.5

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
mas_oz2k1
  • 2,851
  • 3
  • 34
  • 41
  • 3
    This is the only thing that worked for me when trying to run tests using ReSharper 5.1's test runner from Visual Studio 2010 SP1. – Jim Raden May 23 '11 at 15:03
  • 5
    You can also use `RequiresSTA` on individual test methods, and the BCL `STAThread` attribute works as a synonym for `RequiresSTA`. – Alex Humphrey Feb 15 '12 at 11:23
  • 1
    What is the difference between setting it in the app.config and as an attribute in the assembly? In my case, both make the tests report as running in STA, however only setting the attribute seems to make the tests that need to be run on the same thread pass. Is it a case that the config ensures each TestFixture is run on separate threads sequentially and the assembly level attribute makes the entire test batch run on a single thread? – Zepee Oct 06 '15 at 09:58
  • 19
    `[RequiteSTA]` _now_ obsolete, Use `[Apartment(ApartmentState.STA)]`. – bvj May 30 '17 at 02:54
  • Now you should use `[RequiresThread(ApartmentState.STA)]`. – FredM Jun 01 '21 at 06:22
90

NUnit 3.0

We migrated to NUnit 3.0 recently and the old attributes that we had been using no longer worked. Our tests used a mixture of [STAThread] and [RequiresSTA] as in mas_oz2k1's answer above. STAThread was giving compile errors since it was no longer found and RequiresSTA was giving warnings because it has been deprecated.

The New Deal appears to be using the following:

Assembly Level

[assembly: Apartment(ApartmentState.STA)]

Class Level

[TestFixture]
[Apartment(ApartmentState.STA)]

Method Level

[Test]
[Apartment(ApartmentState.STA)]

Trying to find this information took me down a dark road where people were modifying their test code using a class called CrossThreadTestRunner. This was the solution in 2004 I assume, before these attribute classes were created.

JoelC
  • 3,664
  • 9
  • 33
  • 38
35

For NUnit 2.2, 2.4 (See simple solution below for 2.5):

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");
}
GregC
  • 7,737
  • 2
  • 53
  • 67
Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
  • 6
    Simple, yet frustrating, question, immediate working answer : some days I just love SO. – Peter Mar 12 '10 at 16:30
  • 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 Mar 12 '10 at 16:34
  • 1
    Surprisingly, it even works when using Resharper to run the tests inside VS2010. Many thanks! – Lee Oades Oct 15 '10 at 10:16
  • Wow, you just fixed something I've been pulling my hair out over for a while. Another one of those times when I wish I could upvote more than once. – mdm Nov 16 '10 at 10:48
  • 2
    Note that the app.config file must end up with a name corresponding to the dll. E.g. MyApp.Tests.dll.config for MyApp.Tests.dll. VS will do this for you automatically, but if you're having to manually create the config file like me, remember this step. – Jason Crease Jan 10 '12 at 11:44
  • 1
    On Linux/mono one needs to use type="System.Configuration.NameValueSectionHandler, System" – Tom Sep 25 '12 at 20:18
4

In NUnit 2.6.1+ you can use the /apartment=STA command line flag:

NUnit-Console version 2.6.3.13283
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
   OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1
  CLR Version: 4.0.30319.18052 ( Net 4.5 )


NUNIT-CONSOLE [inputfiles] [options]

Runs a set of NUnit tests from the console.

You may specify one or more assemblies or a single
project file of type .nunit.

Options:
...
/apartment=X            Apartment for running tests: MTA (Default), STA
...
Denis
  • 11,796
  • 16
  • 88
  • 150