gpt4 book ai didi

c# - 为什么我的某些单元测试会运行多次?

转载 作者:太空狗 更新时间:2023-10-30 01:00:01 24 4
gpt4 key购买 nike

当我使用 Visual Studio 的内置测试引擎运行我的解决方案中的所有单元测试时,我会看到一些测试运行了多次,而其他测试只运行了一次:

Test Result Window

看来,如果同一个文件中有多个 TestClass,事情就会变得困惑。在这个文件中,例如:

[TestClass]
public class AerationEngineTests
{
protected IAerationEngine aerationEngine;
protected AerationPackage aerationPackage;
protected BinPackage bin;

[TestInitialize]
public void TestInitialize()
{
aerationEngine = new AerationEngine();
aerationPackage = new AerationPackage();
bin = new BinPackage();
bin.Children.Add(new Shell {ShellDiameter = 75, RingCount = 15, RingHeight = RingHeights.RingHeight32});
}

[TestMethod]
public void ShouldCalculateFloorArea()
{
// Arrange


// Act
var floorArea = aerationEngine.CalculateFloorArea(aerationPackage, bin);

// Assert
floorArea.ShouldBeInRange(4417.86, 4417.87);
}

[TestMethod]
public void ShouldCalculateBinCapacity()
{
// Arrange
aerationPackage.FillType = FillTypes.Peaked;
var grainDepth = aerationEngine.CalculateGrainDepth(aerationPackage, bin);
var floorArea = aerationEngine.CalculateFloorArea(aerationPackage, bin);
var calculations = new FanCalculations {GrainDepth = grainDepth, FloorArea = floorArea};

// Act
var binCapacity = aerationEngine.CalculateMaxBinCapacity(calculations);

// Assert
binCapacity.ShouldBeInRange(175525.88, 175525.9);
}

[TestMethod]
public void ShouldCalculateTargetAirFlow()
{
// Arrange
aerationPackage.FillType = FillTypes.Peaked;
aerationPackage.DesiredAirFlow = AirFlowRates.AirFlowOne4th;
var grainDepth = aerationEngine.CalculateGrainDepth(aerationPackage, bin);
var floorArea = aerationEngine.CalculateFloorArea(aerationPackage, bin);
var calculations = new FanCalculations {GrainDepth = grainDepth, FloorArea = floorArea};
var binCapacity = aerationEngine.CalculateMaxBinCapacity(calculations);
calculations.MaxBinCapacity = binCapacity;

// Act
var airflow = aerationEngine.CalculateTargetAirflow(aerationPackage, calculations);

// Assert
airflow.ShouldBeInRange(43881.46, 43881.48);
}

[TestMethod]
public void ShouldCalculateTargetAirFlowPerFan()
{
// Arrange
aerationPackage.FillType = FillTypes.Peaked;
aerationPackage.DesiredAirFlow = AirFlowRates.AirFlowOne4th;
aerationPackage.FanCount = 4;
var grainDepth = aerationEngine.CalculateGrainDepth(aerationPackage, bin);
var floorArea = aerationEngine.CalculateFloorArea(aerationPackage, bin);
var calculations = new FanCalculations {GrainDepth = grainDepth, FloorArea = floorArea};
var binCapacity = aerationEngine.CalculateMaxBinCapacity(calculations);
calculations.MaxBinCapacity = binCapacity;
var airflow = aerationEngine.CalculateTargetAirflow(aerationPackage, calculations);
calculations.TargetAirFlow = airflow;
// Act
var flowPerFan = aerationEngine.CalculateTargetAirflowPerFan(aerationPackage, calculations);

// Assert
flowPerFan.ShouldBeInRange(10970.3, 10970.4);
}

[TestMethod]
public void ShouldCalculateFloorPressure()
{
// Arrange

// Act
var floorPressure = aerationEngine.CalculateFloorPressure(aerationPackage, bin);

// Assert
floorPressure.ShouldBeInRange(1943.0, 1945.0);
}

[TestMethod]
public void ShouldCalculateTrenchWidth()
{
// Arrange
aerationPackage.FillType = FillTypes.Peaked;
aerationPackage.DesiredAirFlow = AirFlowRates.AirFlowOne4th;
aerationPackage.FanCount = 4;
var grainDepth = aerationEngine.CalculateGrainDepth(aerationPackage, bin);
var floorArea = aerationEngine.CalculateFloorArea(aerationPackage, bin);
var calculations = new FanCalculations {GrainDepth = grainDepth, FloorArea = floorArea};
var binCapacity = aerationEngine.CalculateMaxBinCapacity(calculations);
calculations.MaxBinCapacity = binCapacity;
var airflow = aerationEngine.CalculateTargetAirflow(aerationPackage, calculations);
calculations.TargetAirFlow = airflow;
var flowPerFan = aerationEngine.CalculateTargetAirflowPerFan(aerationPackage, calculations);
calculations.TargetAirFlowPerFan = flowPerFan;

// Act
var trenchWidth = aerationEngine.CalculateTrenchWidth(calculations);

// Assert
trenchWidth.ShouldBe(72);
}
}

[TestClass]
public class CalculateGrainDepthMethod : AerationEngineTests
{
[TestInitialize]
public new void TestInitialize()
{
base.TestInitialize();
}

[TestMethod]
public void ShouldCalculateLevelFillHeight()
{
// Arrange
aerationPackage.FillType = FillTypes.Level;

// Act
var grainDepth = aerationEngine.CalculateGrainDepth(aerationPackage, bin);

// Assert
grainDepth.ShouldBe(40.0);
}

[TestMethod]
public void ShouldCalculatePeakedFillHeight()
{
// Arrange
aerationPackage.FillType = FillTypes.Peaked;

// Act
var grainDepth = aerationEngine.CalculateGrainDepth(aerationPackage, bin);

// Assert
grainDepth.ShouldBeInRange(46.64, 46.66);
}
}

CalculateGrainDepthMethod 中的测试运行一次,但其余测试方法运行两次,它们被分组在 CalculateGrainDepthMethod 下。请参阅此图片(使用 ReSharper 的工具):

ReSharper Test result window

似乎没有同时运行两个测试引擎,但也许有?或者也许我只是错误地构建了测试本身?有谁知道为什么这些测试会运行不止一次,以及为什么它们会被分组到它们不属于的类下?

最佳答案

您正在继承类 CalculateGrainDepthMethod 继承自 AerationEngineTests。因为类 AerationEngineTests 中的这个测试执行了两次。

[TestClass]
public class CalculateGrainDepthMethod : AerationEngineTests
{

要么在类 CalculateGrainDepthMethod 中包含依赖项,要么创建一个测试助手来初始化这些值并在两个测试类中使用它。

关于c# - 为什么我的某些单元测试会运行多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50141891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com