簡體   English   中英

無論如何強制NUnitlite在主線程中運行?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM