简体   繁体   中英

Why are some of my Unit Tests being run multiple times?

When I run all of the Unit Tests in my solution using Visual Studio's built-in testing engine, I'll see that some tests are run multiple times, while others are only run once:

Test Result Window

It appears that if there are multiple TestClass es in the same file, things get confusing. In this file, for example:

[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);
    }
}

The tests inside of CalculateGrainDepthMethod get run once, but the rest of the test methods get run twice, and they're grouped under CalculateGrainDepthMethod . See this pic (using ReSharper's tools):

ReSharper Test result window

It doesn't appear that there are two testing engines running at the same time, but maybe there are? Or maybe I just structured the tests themselves incorrectly? Does anyone know why these tests would run more than once, and why they'd be grouped under classes to which they don't belong?

You are inheriting class CalculateGrainDepthMethod inherits from AerationEngineTests . Because of this tests in class AerationEngineTests are executing twice.

[TestClass]
public class CalculateGrainDepthMethod : AerationEngineTests
{

Either have the dependencies contained in class CalculateGrainDepthMethod or create a test helper which initializes these values and use it in both your test classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM