gpt4 book ai didi

c# - 单元测试需要更多时间做减法

转载 作者:行者123 更新时间:2023-11-30 15:56:15 26 4
gpt4 key购买 nike

我正在通过编写简单示例来尝试单元测试。

测试代码:

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-9 毫秒,而其他测试花费不到 1 毫秒,你能解释一下为什么吗?

测试结果:

enter image description here

最佳答案

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

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

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

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

关于c# - 单元测试需要更多时间做减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47154239/

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