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

时间:2018-05-02 19:10:40

标签: c# unit-testing visual-studio-2015 resharper

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

Test Result Window

看来如果同一个文件中有多个TestClass es,就会让人感到困惑。在此文件中,例如:

[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

似乎没有两个测试引擎同时运行,但可能有?或者我可能只是错误地构建了测试?有谁知道为什么这些测试会不止一次运行,为什么他们会被归入他们不属于的课程之列?

1 个答案:

答案 0 :(得分:5)

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

[TestClass]
public class CalculateGrainDepthMethod : AerationEngineTests
{

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