[英]Anyway to force NUnitlite run in the main thread?
在 NUnit/Nunitlite 中,是否可以強制測試在主線程中運行?
我已經將我的測試裝飾為Apartment(ApartmentState.STA)
,但它仍然在單獨的線程中運行,如下所示:
這是我的代碼:
[TestFixture, Apartment(ApartmentState.STA)]
public class MetaClass
{
[Test]
public void RunA()
{
Assert.AreEqual(1,1);
}
}
[Apartment(ApartmentState.STA)]
internal class Program
{
static void Main(string[] args)
{
string directoryPlugin = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string directoryReportUnit = Path.Combine(directoryPlugin, @"ReportUnit");
Directory.CreateDirectory(directoryReportUnit);
string fileInputXML = Path.Combine(directoryReportUnit, @"Report-NUnit.xml");
string[] nunitArgs = new List<string>
{
"--trace=verbose" // Tell me everything
,"--result=" + fileInputXML
}.ToArray();
new AutoRun().Execute(nunitArgs);
}
}
NUnit 的ApartmentAttribute
與您正在做的ApartmentState.STA
一起使用,強制線程在單線程單元中運行。 此外,它的副作用是 NUnit 調度程序一次只能運行一個這樣的測試......即它使用非並行測試工作線程。 但是,它與 STA 中運行的線程無關。 所以不要使用期望影響線程使用的屬性。
框架如何使用線程由命令行上的--workers
選項控制。 我不是 100% 確定,因為這會根據您使用的框架版本而有所不同,但我相信--workers=-1
會讓您更接近您想要的,因為它完全消除了並行調度程序的使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.