繁体   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