单元测试复合函数 [英] Unit testing composite functions

查看:19
本文介绍了单元测试复合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有 3 个函数,functionA、functionB 和 functionC

Say you have 3 functions, functionA, functionB, and functionC

functionC 依赖于 functionA 和 functionB

functionC relies on functionA and functionB

functionA(a) {
    return a
}
functionB(b) {
     return b
}
functionC(a, b){
     return functionA(a) + functionB(b);
}

现在这显然是一个超级简化的例子..但是测试functionC的正确方法是什么?如果我已经在测试 functionA 和 functionB 并且他们正在通过,那么测试 functionC 就不会不仅仅是一个单元测试,因为它将依赖于 functionA 和 functionB 返回..

Now this is obviously a super simplified example.. but what's the correct way to test functionC? If I'm already testing functionA and functionB and theyre passing wouldn't testing functionC wind up being more than a unit test since it would be relying on functionA and functionB returns..

推荐答案

对于前两个函数,您将专注于它们的公共契约 - 您将根据需要编写尽可能多的测试,以确保不同情况下的所有结果都符合预期.

For the first two functions you would focus on their public contract - you would write as many tests as required to ensure all results for different cases are as expected.

但是对于第三个函数,理解该函数应该调用其他两个函数可能就足够了.所以你可能需要更少的测试.您不需要再次测试测试 A 和 B 所需的所有案例.您只想验证 C 负责的预期管道".

But for the third function it might be sufficient to understand that the function should be invoking the other two functions. So you probably need less tests. You don't need to test again all cases required for testing A and B. You only want to verify the expected "plumbing" that C is responsible for.

这篇关于单元测试复合函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆