English 中文(简体)
确保以基本等级安全平行执行与共同目的初始化的试验
原标题:Ensuring Safe Parallel Execution of Tests with Shared Object Initialization in Base Class
  • 时间:2024-05-22 16:06:05
  •  标签:
  • .net
  • nunit

我在使用 NUnit 进行测试的.NET 工程中工作。 我有一个基础测试等级 < engreencode> BaseA , 它将一个共享对象初始化为构建器。 我衍生的等级 < generalcode> A 包含几种测试方法。 我使用“ pararallable > [Parallable( ParallelScope. self.] 属性, 允许在本类中平行进行测试。 以下是我设置的简化版本 :

public class BaseA
{
    protected IMyInterface sharedObject;

    public BaseA()
    {
        sharedObject = new MyClass(); // Initialization in the base constructor
    }
}

[TestFixture]
[Parallelizable(ParallelScope.Self)]
public class A : BaseA
{
    [Test]
    public void Test1()
    {
        sharedObject.Method1();
        // Other operations with sharedObject
    }

    [Test]
    public void Test2()
    {
        sharedObject.Method2();
        // Other operations with sharedObject
    }

    // Additional tests
}

我希望确保每种测试方法都获得自己的“强{代码>共享Object

  1. > 是否每种测试方法都有新的 共享Object 实例?

  2. 如果不是,什么是修改设置以确保每种测试方法获得单独实例的最佳方式?

我考虑过使用“坚固”代码 方法初始化“坚固”代码(Object ),但我不确定这是否是最佳方法。

public class BaseA
{
    protected IMyInterface sharedObject;

    [SetUp]
    public void BaseSetup()
    {
        sharedObject = new MyClass(); // Initialization before each test
    }
}

[TestFixture]
[Parallelizable(ParallelScope.Self)]
public class A : BaseA
{
    [Test]
    public void Test1()
    {
        sharedObject.Method1();
        // Other operations with sharedObject
    }

    [Test]
    public void Test2()
    {
        sharedObject.Method2();
        // Other operations with sharedObject
    }

    // Additional tests
}

这种方法能否确保每种测试方法都获得一个单独的“Object ”实例,使测试对平行执行安全?是否为这一设想方案建议了任何其他做法?

谢谢你的帮助!

问题回答

由于每个测试案例之前都使用[setUp] 方法, 您的每个测试案例将获得一个共享对象的单独实例。 如果共享对象持有任何未管理的资源, 您应该用 [ 方法处置它 。

然而, 您的可平行属性不允许方法平行运行。 平行的Scope. self( 默认值) 意味着属性适用于它所显示的测试, 在此情况下, 您的固定值。 因此它表示, 您的固定值可能与其他固定装置平行运行, 而单个方法则处于默认值 。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签