【问题标题】:How to run unit tests in STAThread mode?如何在 STAThread 模式下运行单元测试?
【发布时间】:2011-01-26 21:42:30
【问题描述】:

我想测试一个使用剪贴板 (WindowsForms) 的应用程序,并且我的单元测试中也需要剪贴板。为了使用它,它应该在 STA 模式下运行,但是由于 NUnit TestFixture 没有 main 方法,我不知道在哪里/如何注释它。

【问题讨论】:

  • 出于好奇,在 TextFixture/TestMethod/TestClass 之上添加属性 [STATThread] 是否不够?
  • 它只适用于方法,它不适用于fixturesetup、testmethods……当然我可能忽略了某事。如果您找到其他解决方案,欢迎您回答。

标签: c# nunit sta


【解决方案1】:

如果您使用的是 nunit 2.5+,您可以在课堂上使用新的 RequiresSTAAttribute

[TestFixture, RequiresSTA]

或装配级别。

[assembly:RequiresSTA]

不需要配置文件。查看: http://www.nunit.org/index.php?p=requiresSTA&r=2.5

【讨论】:

  • 这是我在尝试使用 Visual Studio 2010 SP1 中的 ReSharper 5.1 的测试运行程序运行测试时唯一有效的方法。
  • 您也可以在个别测试方法上使用RequiresSTA,BCL STAThread 属性用作RequiresSTA 的同义词。
  • 在 app.config 中设置它和在程序集中作为属性有什么区别?在我的情况下,两者都使测试报告为在 STA 中运行,但是仅设置属性似乎使需要在同一线程上运行的测试通过。配置是否确保每个 TestFixture 按顺序在单独的线程上运行,并且程序集级别属性使整个测试批处理在单个线程上运行?
  • [RequiteSTA] 现在已过时,使用[Apartment(ApartmentState.STA)]
  • 现在你应该使用[RequiresThread(ApartmentState.STA)]
【解决方案2】:

NUnit 3.0

我们最近迁移到 NUnit 3.0,我们一直使用的旧属性不再起作用。我们的测试使用了[STAThread][RequiresSTA] 的混合物,如上面 mas_oz2k1 的回答。 STAThread 因不再被发现而产生编译错误,而 RequiresSTA 因已被弃用而发出警告。

新政似乎正在使用以下内容:

装配级别

[assembly: Apartment(ApartmentState.STA)]

班级等级

[TestFixture]
[Apartment(ApartmentState.STA)]

方法级别

[Test]
[Apartment(ApartmentState.STA)]

试图找到这些信息让我走上了一条黑暗的道路,人们正在使用一个名为 CrossThreadTestRunner 的类来修改他们的测试代码。这是我假设的 2004 年的解决方案,在创建这些属性类之前。

【讨论】:

  • 这是 NUnit 3.0+ 的 the 答案。不要忘记包含System.Threading
【解决方案3】:

对于 NUnit 2.2、2.4(参见下面的 2.5 简单解决方案):

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

您可以使用以下 C# 代码验证单元线程是否为 STA:

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

【讨论】:

  • 简单但令人沮丧的问题,即时有效的答案:有些日子我只是喜欢这样。
  • 如果您再次寻找它(并且 SO 已关闭),这来自 WatiN 中提供的示例配置。很高兴我能帮助你。 :)
  • 令人惊讶的是,它甚至在使用 Resharper 在 VS2010 中运行测试时也能正常工作。非常感谢!
  • 请注意,app.config 文件必须以与 dll 对应的名称结尾。例如。 MyApp.Tests.dll.config 用于 MyApp.Tests.dll。 VS 会自动为你做这件事,但如果你必须像我一样手动创建配置文件,请记住这一步。
  • 在 Linux/mono 上需要使用 type="System.Configuration.NameValueSectionHandler, System"
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多