Multithreading 运行多线程nunit测试的最佳方法

Multithreading 运行多线程nunit测试的最佳方法,multithreading,unit-testing,nunit,Multithreading,Unit Testing,Nunit,我目前正试图找到一个解决方案,如何确保在测试方法产生的线程中发生异常时测试失败 我不想在单元测试中讨论多个线程。=>“单元测试”。替换(“单元”、“集成”) 我已经在几个论坛上阅读了很多线程,我知道,但是我正在寻找一个集成到nunit中的解决方案,并且不需要重写很多测试。我通过为nunit创建一个加载项来解决这个问题,它“安装”了一个ITestDecorator。非测试线程上出现异常的原因(即其他衍生线程)不会导致测试失败的原因是NUnit默认配置为使用,这是一个.Net进程级别设置,可通过ap

我目前正试图找到一个解决方案,如何确保在测试方法产生的线程中发生异常时测试失败

我不想在单元测试中讨论多个线程。=>“单元测试”。替换(“单元”、“集成”)


我已经在几个论坛上阅读了很多线程,我知道,但是我正在寻找一个集成到nunit中的解决方案,并且不需要重写很多测试。

我通过为nunit创建一个加载项来解决这个问题,它“安装”了一个ITestDecorator。

非测试线程上出现异常的原因(即其他衍生线程)不会导致测试失败的原因是NUnit默认配置为使用,这是一个.Net进程级别设置,可通过app.config应用,即:

<legacyUnhandledExceptionPolicy enabled="1"/>

启用此设置(即将其设置为“1”)会导致忽略主线程上未发生的异常

我写了一篇关于ReSharper测试运行程序的文章,其中更详细地介绍了这个问题,但它同样适用于NUnit测试运行程序:


我也遇到了同样的问题,我的解决方案是捕获异常并增加一个异常计数器,因此测试方法只需断言异常计数器为0,以确认没有线程得到异常

删除特定环境内容后的测试代码摘录:

    const int MaxThreads = 25;
    const int MaxWait = 10;
    const int Iterations = 10;
    private readonly Random random=new Random();
    private static int startedThreads=MaxThreads ;
    private static int exceptions = 0;

[测试]
公共void testclass()
{
//创建n个线程,每个线程都将读取配置,而另一个线程将进行清理
线程=新线程(方法1)
{
IsBackground=true,
Name=“MyThread0”
};
thread.Start();
对于(int i=1;i0&&exceptions==0)
{
线程睡眠(MaxWait);
}
AreEqual(0,异常,“线程上不应出现异常”);
}
私有void方法1()
{
尝试
{
对于(int i=0;i
这对我不起作用,测试仍然通过。但它确实像文章所说的那样扼杀了测试。这是一个怎样的答案?请提供一些信息,说明你做了什么使它起作用。这根本不是问题的答案。@Martin Moser请详细说明
[Test]
    public void testclass()
    {
        // Create n threads, each of them will be reading configuration while another one cleans up

        Thread thread = new Thread(Method1)
        {
            IsBackground = true,
            Name = "MyThread0"
        };

        thread.Start();
        for (int i = 1; i < MaxThreads; i++)
        {
            thread = new Thread(Method2)
            {
                IsBackground = true,
                Name = string.Format("MyThread{0}", i)
            };

            thread.Start();
        }

        // wait for all of them to finish
        while (startedThreads > 0 && exceptions==0)
        {
            Thread.Sleep(MaxWait);
        }
        Assert.AreEqual(0, exceptions, "Expected no exceptions on threads");
    }

    private void Method1()
    {
        try
        {
            for (int i = 0; i < Iterations; i++)
            {
            // Stuff being tested
                Thread.Sleep(random.Next(MaxWait));
            }
        }
        catch (Exception exception)
        {
            Console.Out.WriteLine("Ërror in Method1 Thread {0}", exception);
            exceptions++;
        }
        finally
        {
            startedThreads--;
        }
    }

    private void Method2()
    {
        try
        {
            for (int i = 0; i < Iterations; i++)
            {
                // Stuff being tested
                Thread.Sleep(random.Next(MaxWait));
            }
        }
        catch (Exception exception)
        {
            Console.Out.WriteLine("Ërror in Method2 Thread {0}", exception);
            exceptions++;
        }
        finally
        {
            startedThreads--;
        }
    }