带有并行测试用例的.NET测试框架

时间:2016-11-02 21:32:42

标签: c# unit-testing testing nunit xunit

我在C#项目(VisualStudio 2015Nunit)中进行了单元测试。我想检查具有不同参数的许多不同情况下的方法,为此,我使用了TestCases。每种测试方法都非常慢 - 几秒钟,我的项目中TestCases计数增长。

所以现在测试需要几分钟。我尝试将这些测试并行,将每个测试分开TestFixture - Nunit允许并行运行TestFixtures - Nunit Documentation。它略微改变了情况,但并不显着。我希望并行测试不在TestFixture级别,而是在测试用户级别上。我不知道怎么做。我已阅读NunitXunitMBUnit等文档,但未找到答案。

如何以并行方式运行测试,以便测试用例同时运行?使用哪个框架?

我在伪代码中的测试示例:

public static class GeneralTestCases
{
    public static IEnumerable TestStoresCredentials
    {
        get
        {
            yield return new TestCaseData( ... ).SetName("incorrect password");
            ...
            yield return new TestCaseData( ... ).SetName("incorrect login");
        }
    }
}

[ TestFixture ]
[ Parallelizable ]
internal class GetProducts : BaseTest
{
    [ Test ]
    [ TestCaseSource( typeof( GeneralTestCases ), "TestCases" ) ]
    public void ReceiveProducts( TestCase case )
    {
        // ------------ Arrange
        var service = CreateService( case.User, case.Key, ... );

        // ------------ Act
        var serviceResponse = service.GetProducts(case.SpecialType);

        // ------------ Assert
        ...
        serviceResponse.Message.Should().NotBeNullOrEmpty();
        ...
    }
}


[ TestFixture ]
[ Parallelizable ]
internal class GetOrders : BaseTest
{
    [ Test ]
    [ TestCaseSource( typeof( GeneralTestCases ), "TestCases" ) ]
    public void ReceiveOrders( TestCase case )
    {
        // ------------ Arrange
        var service = CreateService( case.User, case.Key, ... );

        // ------------ Act
        var serviceResponse = service.GetOrders(case.SpecialType);

        // ------------ Assert
        ...
        serviceResponse.Message.Should().NotBeNullOrEmpty();
        ...
    }
}

enter image description here

UPDATE1 : 我正在考虑nCrunch,但我不会帮助或不是

3 个答案:

答案 0 :(得分:0)

默认情况下,

xUnit 并行运行每个测试类(测试集合)。

更多信息here

答案 1 :(得分:0)

经过一些研究,我找到了两种同时运行测试的方法。

  1. Nunit + Ncrunch它允许并行运行测试(测试用例 并行化支持)。但对我来说这种方式并不方便。一世 不喜欢Ncrunch GUI,需要时间才能正确配置它 方式(长时间运行测试阈值设置,多线程设置 等等)。 Ncrunch也是免费的。
  2. Nunit + Nunit tests runner console。我选择这种方式。它允许 并行运行测试,它是免费的,易于配置,可以运行 从控制台。您可以配置为在1个conosle窗口中并行运行大量测试,或者运行多个控制台,每个控制台每次运行1次测试

答案 2 :(得分:0)

我已经使用NUnit完成了这项工作。我有一个工作项目,它在方法级别执行浏览器测试。

https://github.com/atmakur/WebTestingFramework