简体   繁体   English

如何在STAThread模式下运行单元测试?

[英]How to run unit tests in STAThread mode?

I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my unit tests also. 我想测试一个使用剪贴板(WindowsForms)的应用程序,我也需要在单元测试中使用剪贴板。 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. 为了使用它,它应该在STA模式下运行,但由于NUnit TestFixture没有main方法,我不知道在哪里/如何注释它。

If you are using nunit 2.5+, you can use the new The RequiresSTAAttribute at class 如果您使用的是nunit 2.5+,则可以在课堂上使用新的RequiresSTAAttribute

[TestFixture, RequiresSTA]

or assembly level. 或装配水平。

[assembly:RequiresSTA]

No need for config file. 不需要配置文件。 check: http://www.nunit.org/index.php?p=requiresSTA&r=2.5 检查: http//www.nunit.org/index.php?p = requiresSTA& r = 2.5

NUnit 3.0 NUnit 3.0

We migrated to NUnit 3.0 recently and the old attributes that we had been using no longer worked. 我们最近迁移到NUnit 3.0,我们使用的旧属性不再有效。 Our tests used a mixture of [STAThread] and [RequiresSTA] as in mas_oz2k1's answer above. 我们的测试使用了[STAThread][RequiresSTA]的混合,如上面mas_oz2k1的答案。 STAThread was giving compile errors since it was no longer found and RequiresSTA was giving warnings because it has been deprecated. STAThread因为不再找到而导致编译错误,而且RequiresSTA正在发出警告,因为它已被弃用。

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. 试图找到这些信息让我走上了一条黑暗的道路,人们正在使用一个名为CrossThreadTestRunner的类来修改他们的测试代码。 This was the solution in 2004 I assume, before these attribute classes were created. 这是2004年我假设的解决方案,在创建这些属性类之前。

For NUnit 2.2, 2.4 (See simple solution below for 2.5): 对于NUnit 2.2,2.4(参见下面2.5的简单解决方案):

Add an app.config file to the project containing your unit tests and include the following: 将app.config文件添加到包含单元测试的项目中,并包含以下内容:

<?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: 您可以使用以下C#代码验证公寓线程是否为STA:

if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
   throw new ThreadStateException("The current threads apartment state is not STA");
}

In NUnit 2.6.1+ you can use the /apartment=STA command line flag: 在NUnit 2.6.1+中,您可以使用/ apartment = STA命令行标志:

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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM