[英]How to run mspec with resharper runner in STAThread mode
[英]How to run unit tests in STAThread mode?
我想測試一個使用剪貼板(WindowsForms)的應用程序,我也需要在單元測試中使用剪貼板。 為了使用它,它應該在STA模式下運行,但由於NUnit TestFixture
沒有main方法,我不知道在哪里/如何注釋它。
如果您使用的是nunit 2.5+,則可以在課堂上使用新的RequiresSTAAttribute
[TestFixture, RequiresSTA]
或裝配水平。
[assembly:RequiresSTA]
不需要配置文件。 檢查: http : //www.nunit.org/index.php?p = requiresSTA& r = 2.5
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 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");
}
在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.