单元测试需要更多时间进行减法

时间:2017-11-07 09:27:58

标签: c# unit-testing

我通过编写简单的例子来尝试单元测试。

经过测试的代码:

public class Operations
{
    public static double Subtraction(double number1, double number2)
    {
        return number1 - number2;
    }

    public static double Addition(double number1, double number2)
    {
        return number1 + number2;
    }

    public static double Division(double number1, double number2)
    {
        if (number2 != 0)
        {
            return number1 / number2;
        }
        else
        {
            return number1;
        }
    }

    public static double Multiplication(double number1, double number2)
    {
        return number1 * number2;
    }
}

测试方法:

[TestClass()]
public class OperationsTests
{
    [TestMethod()]
    public void SubtractionTest1()
    {
        double number1 = 0;
        double number2 = 10;
        double result = Operations.Subtraction(number1, number2);
        Assert.AreEqual(-10, result);
    }

    [TestMethod()]
    public void SubtractionTest2()
    {
        double number1 = 10;
        double number2 = 0;
        double result = Operations.Subtraction(number1, number2);
        Assert.AreEqual(10, result);
    }

    [TestMethod()]
    public void SubtractionTest3()
    {
        double number1 = 10;
        double number2 = 10;
        double result = Operations.Subtraction(number1, number2);
        Assert.AreEqual(0, result);
    }

    [TestMethod()]
    public void AdditionTest1()
    {
        double number1 = 0;
        double number2 = 10;
        double result = Operations.Addition(number1, number2);
        Assert.AreEqual(10, result);
    }

    [TestMethod()]
    public void AdditionTest2()
    {
        double number1 = 10;
        double number2 = 0;
        double result = Operations.Addition(number1, number2);
        Assert.AreEqual(10, result);
    }

    [TestMethod()]
    public void AdditionTest3()
    {
        double number1 = 10;
        double number2 = 10;
        double result = Operations.Addition(number1, number2);
        Assert.AreEqual(20, result);
    }

    [TestMethod()]
    public void DivisionTest1()
    {
        double number1 = 0;
        double number2 = 10;
        double result = Operations.Division(number1, number2);
        Assert.AreEqual(0, result);
    }

    [TestMethod()]
    public void DivisionTest2()
    {
        double number1 = 10;
        double number2 = 0;
        double result = Operations.Division(number1, number2);
        Assert.AreEqual(10, result);
    }

    [TestMethod()]
    public void DivisionTest3()
    {
        double number1 = 10;
        double number2 = 10;
        double result = Operations.Division(number1, number2);
        Assert.AreEqual(1, result);
    }

    [TestMethod()]
    public void MultiplicationTest1()
    {
        double number1 = 0;
        double number2 = 10;
        double result = Operations.Multiplication(number1, number2);
        Assert.AreEqual(0, result);
    }

    [TestMethod()]
    public void MultiplicationTest2()
    {
        double number1 = 10;
        double number2 = 0;
        double result = Operations.Multiplication(number1, number2);
        Assert.AreEqual(0, result);
    }

    [TestMethod()]
    public void MultiplicationTest3()
    {
        double number1 = 10;
        double number2 = 10;
        double result = Operations.Multiplication(number1, number2);
        Assert.AreEqual(100, result);
    }
}

每个测试都在执行而没有任何错误但是我注意到SubtractionTest1总是花费7-8-9ms,而其他测试花费不到1ms,你能解释一下为什么吗?

测试结果:

enter image description here

1 个答案:

答案 0 :(得分:3)

MSTest将随机化单元测试运行的顺序,因为这有时会暴露时间或共享状态中的错误。

首先运行的测试将花费更长的时间,因为它将是JIT编译每个方法和类型所需的一个,如果这些类型中的任何一个都有静态初始化要执行,那么这也会触发。

我有一个单元测试套件,第一次测试总是花费大约0.5秒,因为我正在初始化依赖注入和模拟框架。每个测试后初始化大约需要1ms到2ms。所以你在这里看到的行为是完全正常的。

对于性能测试,您必须对结果进行平均并弃掉您的第一个结果,以便从中删除初始化时间。如果这与您的应用程序相关,您还应该测试初始化​​速度,但这通常与启动一次然后保持在线并准备就绪的服务器无关。