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

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

问题描述

我正在尝试对我编写的 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 类,由微软编写.我没有能力改变它的行为.将每个服务端点仅托管一次也是正确的行为……但是,这对单元测试特别不利.

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天全站免登陆