串行执行单元测试(而不是并行执行) [英] Execute unit tests serially (rather than in parallel)

查看:370
本文介绍了串行执行单元测试(而不是并行执行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我编写的WCF主机管理引擎进行单元测试.引擎基本上是根据配置动态创建ServiceHost实例.这样一来,我们就可以动态地重新配置可用的服务,而不必关闭所有服务并在添加新服务或删除旧服务时重新启动它们.

I am attempting to unit test a WCF host management engine that I have written. The engine basically creates ServiceHost instances on the fly based on configuration. This allows us to dynamically reconfigure which services are available without having to bring all of them down and restart them whenever a new service is added or an old one is removed.

但是,由于ServiceHost的工作方式,我在对主机管理引擎进行单元测试时遇到了困难.如果已经为某个特定端点创建,打开了ServiceHost,但尚未关闭它,则无法为该端点创建另一个ServiceHost,从而导致异常.由于现代单元测试平台可以并行执行其测试执行,因此我没有有效的方法来对这段代码进行单元测试.

I have run into a difficulty in unit testing this host management engine, however, due to the way ServiceHost works. If a ServiceHost has already been created, opened, and not yet closed for a particular endpoint, another ServiceHost for the same endpoint can not be created, resulting in an exception. Because of the fact that modern unit testing platforms parallelize their test execution, I have no effective way to unit test this piece of code.

我使用过xUnit.NET,希望由于它的可扩展性,我可以找到一种方法来强制其顺序运行测试.但是,我没有任何运气.我希望有人在SO上遇到类似的问题,并且知道如何使单元测试连续运行.

I have used xUnit.NET, hoping that because of its extensibility, I could find a way to force it to run the tests serially. However, I have not had any luck. I am hoping that someone here on SO has encountered a similar issue and knows how to get unit tests to run serially.

注意: ServiceHost 是WCF类,由Microsoft编写.我没有能力改变它的行为.每个服务端点仅托管一次也是一种正确的行为……但是,这特别不利于单元测试.

NOTE: ServiceHost is a WCF class, written by Microsoft. I don't have the ability to change it's behavior. Hosting each service endpoint only once is also the proper behavior...however, it is not particularly conducive to unit testing.

推荐答案

每个测试类都是唯一的测试集合,并且其下的测试将按顺序运行,因此,如果将所有测试放在同一集合中,则它将按顺序运行

Each test class is a unique test collection and tests under it will run in sequence, so if you put all of your tests in same collection then it will run sequentially.

在xUnit中,您可以进行以下更改以实现此目的:

In xUnit you can make following changes to achieve this:

以下将并行运行:

namespace IntegrationTests
{
    public class Class1
    {
        [Fact]
        public void Test1()
        {
            Console.WriteLine("Test1 called");
        }

        [Fact]
        public void Test2()
        {
            Console.WriteLine("Test2 called");
        }
    }

    public class Class2
    {
        [Fact]
        public void Test3()
        {
            Console.WriteLine("Test3 called");
        }

        [Fact]
        public void Test4()
        {
            Console.WriteLine("Test4 called");
        }
    }
}

要使其具有顺序性,您只需要将两个测试类放在同一集合中即可

namespace IntegrationTests
{
    [Collection("Sequential")]
    public class Class1
    {
        [Fact]
        public void Test1()
        {
            Console.WriteLine("Test1 called");
        }

        [Fact]
        public void Test2()
        {
            Console.WriteLine("Test2 called");
        }
    }

    [Collection("Sequential")]
    public class Class2
    {
        [Fact]
        public void Test3()
        {
            Console.WriteLine("Test3 called");
        }

        [Fact]
        public void Test4()
        {
            Console.WriteLine("Test4 called");
        }
    }
}

有关更多信息,您可以参考此链接

For more info you can refer to this link

这篇关于串行执行单元测试(而不是并行执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆