分支机构覆盖范围
在编写应该具有 100% 分支覆盖率的测试用例时,是否可以让一个用例覆盖两个分支,而另一个用例仅覆盖一个分支。
注意:我们假设代码中只有三个分支。
编辑:3 个分支意味着三个基本 if 语句,它们在代码体内彼此独立。 例如,
input (x, y)
if (x<0)
something
if (x==y)
something
if (x > y)
something
output (x)
我有一个涵盖第一个分支的测试用例和一个涵盖其他两个分支的测试用例
When writing test cases which are supposed to have 100% branch coverage, is it ok to have one of your cases that covers two branches and another case that only covers one.
note: we are assuming there are only three branches in the code.
edit: 3 branches means three basic if statments that are all seperate to each other within a body of code. e.g.
input (x, y)
if (x<0)
something
if (x==y)
something
if (x > y)
something
output (x)
I have one test case that covers the first branch and one test case that covers the other two branches
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我关注代码的行为。 因此,代码的执行有三种不同的可能方式,因此应该进行三个测试。
可以这样想 - 如果两个分支之一中断,您可能不会意识到,因为测试仍然通过(因为另一个分支仍然有效),但生产代码失败。 不理想。
是的,这确实需要更多时间,但在某些情况下这是值得的......一切都 100% 吗? 也许还没有达到那种极端的程度。
Personally, I focus on the behaviour of the code. As such, there are three different possible ways for the code to be executed as such there should be three tests.
Think of it this way - if one of the two branches breaks, you might be unaware because the test still passes (as the other branch still works) but production code fails. Not ideal.
Yes it does take more time, but in some cases it is worth it... 100% for everything? Maybe not to that level of extremes.
100% 分支机构覆盖? 提出类似要求的人是否有任何现实世界的测试覆盖经验? 根据我的经验,对于相当复杂的项目,获得 75-80% 的代码覆盖率和大约 60-70% 的分支覆盖率是最好的希望。 这些数字通常是原始的、分析前的数字。 在消除无法到达的片段后,它们会上升(约 92-95% 的代码和 80-85% 的分支),例如断言、默认开关情况、“纵深防御”代码路径等。
至于你的问题:测试用例越少越好。 不要忘记,测试不仅需要时间来开发,还需要时间来运行和分析故障。 在您第一次等待整个测试套件完成 4 天后,您很快就会了解到将测试用例数量减少到最低限度的价值,从而增强对覆盖范围的信心。
100% branch coverage? Does the person asking for something like that have any real world test coverage experience? In my experience, for reasonably complex projects, obtaining 75-80% code coverage and around 60-70% branch coverage is the best one can hope for. These numbers are usually the raw, pre-analys, numbers. They go up (~92-95% code and 80-85% branch) after the snippets impossible to reach are eliminated, like Asserts, default switch cases, 'defense in depth' code paths and such.
As for your question: the less test cases you have the better. Don't forget that tests take time not only to develop, but to run and analyze failures too. After you waited your first time you wait 4 days for the whole test suite to finish, you quickly learn the value of reducing your number of test cases to the minimum that gives confidence in the coverage.
测试是我们对产品提出的问题。 每个(书面)分支进行一次测试的想法可能有用,也可能愚蠢。
我对您提供的示例有一些疑问
输入 (x, y)
如果 (x<0)
某事
如果 (x==y)
某事
如果 (x > y)
某事
输出(x)
如果x大于零会发生什么? 你应该会失败吗? 如果 x 小于 y 会发生什么? 某物? 没有什么?
事情是这样的:代码(和分支)(和条件)覆盖是个好主意。 但“覆盖”一条线、一条分支或一个条件是什么意思呢? 是为了确保程序可以工作——也就是说,执行给定的行/分支/条件而不崩溃? 或者是为了确保该程序能够工作?
——迈克尔·B.
A test is a question that we have about the product. The idea of one test per (written) branch might be useful or silly.
I have some questions about the example you give
input (x, y)
if (x<0)
something
if (x==y)
something
if (x > y)
something
output (x)
What happens if x is greater than zero? Are you supposed to fall through? What's supposed to happen if x is less than y? Something? Nothing?
Here's the thing: code (and branch) (and condition) coverage are nice ideas. But what does it mean to "cover" a line or a branch or a condition? Is it to make sure that the program can work--that is, execute a given line/branch/condition without crashing? Or is to to make sure that that program will work?
---Michael B.