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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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
Class Level
Method Level
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.
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:
You can verify that the apartment threading is STA with the following C# code:
If you are using nunit 2.5+, you can use the new The
RequiresSTAAttribute
at classor assembly level.
No need for config file. check: http://www.nunit.org/index.php?p=requiresSTA&r=2.5
In NUnit 2.6.1+ you can use the /apartment=STA command line flag: