最近我一直在阅读 Roy Osherove 的《单元测试的艺术》一书,我在想如何处理这种情况:
假设我有一个大方法,它根据传递的参数通过完成不同的任务来完成一个业务流程:
public void MonsterMehtod(Parameters p)
{
// Some processes to accomplish TASK_A
// ....
// Some processes to accomplish TASK_B
// ....
// Some processes to accomplish TASK_C
// ....
}
我想在这个方法上写测试。
所以,如果我把这个大方法排除在像这样的小方法之外:
public void MonsterMethod(Parameters p)
{
Task_A(p);
Task_B(p); //Can behave different depending on Task_A(p) results
Task_C(p); //Can behave different depending on Task_A(p) and Task_B(p) results
}
我为每个任务编写单元测试,如下所示:
[Test]
public void Task_A_AllPossibleConditionsWithParameterP_ExpectedBehaviours() {}
[Test]
public void Task_B_AllPossibleConditions_ExpectedBehaviours()
{
// Tests all possible expected behaviours based on injected parameters
// Tests all possible expected behaviours based on method Task_A(Prameters p) results
}
[Test]
public void Task_C_AllPossibleConditions_ExpectedBehaviours()
{
// Tests all possible expected behaviours based on injected parameters
// Tests all possible expected behaviours based on method Task_A(Prameters p) results
// Tests all possible expected behaviours based on method Task_B(Prameters p) results
// Tests all possible expected behaviours based on method Task_C(Prameters p) results
}
所以,毕竟,为 MonsterMethod(Parameters p) 编写另一个测试是否有意义,例如:
[Test]
public void MonsterMehtod_AllPossibleParameterConditions_ExpectedBehaviours {}
也许我至少可以编写一个测试来检查是否所有 Task_A()、Task_B()、Task_C() 方法都被调用 但是,虽然我为每个子任务进行了所有单元测试,是否值得为包含所有这些子任务的 MonsterMethod(Parameters p) 进行另一个测试(也许应该称为集成测试而不是单元测试)?
最佳答案
如果您编写一种方法,那么它可能会与您已有的任何其他方法有所不同。然后测试它是否正确地完成了其他事情(正确地组合其他函数的结果)是有用的。
关于c# - 是否值得为调用其他方法的方法编写测试,这些方法 100% 被单个单元测试覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4123758/