简体   繁体   English

为什么这个else条件不满足代码覆盖?

[英]Why does this else condition not meet code coverage?

I'm confused about why the following if/else condition doesn't meet code coverage.我对为什么以下if/else条件不满足代码覆盖率感到困惑。 Specifically the else is getting flagged as not having coverage.特别是else被标记为没有覆盖。

if (myBoolean) {
    myComponent.myElement = 'Hello world';
} else {
    myComponent.myElement = 'Hello everyone';
}

I can refactor this to set the value of myComponent.myElement prior to the if condition, and that seem to pass fine.我可以重构它以在if条件之前设置myComponent.myElement的值,这似乎很好。

myComponent.myElement = 'Hello everyone';
if (myBoolean) {
    myComponent.myElement = 'Hello world';
}

Why wouldn't the else condition pass as well?为什么else条件也不能通过?

The coverage tool records which lines of code your tests cause to be executed.覆盖工具记录您的测试导致执行的代码行。

If your flag is set by reading some input, then the flag is used to decide which branch to take, the test will follow either one path or the other.如果您的标志是通过读取某些输入来设置的,那么该标志将用于决定采用哪个分支,测试将遵循一条路径或另一条路径。 If your code under test checks for invalid inputs, code handling that won't be executed by a test using valid inputs.如果您的测试代码检查无效输入,则使用有效输入的测试不会执行代码处理。 You will need a test for each possible case so that all the paths get covered.您将需要对每种可能的情况进行测试,以便涵盖所有路径。

By the way, the coverage tool is useful not just for the statistics it generates but for confirming the test does what you think it does.顺便说一句,覆盖率工具不仅对它生成的统计数据很有用,而且对确认测试是否按照您的想法行事很有用。 If a test fails you can look at the coverage and see how far it got and which branches were executed.如果测试失败,您可以查看覆盖率并查看它达到了多远以及执行了哪些分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么代码覆盖在 ReSharper 中不起作用? - Why is code coverage not working in ReSharper? 100% 的代码覆盖率是否意味着代码是正确的? - Does 100% code coverage mean that the code is correct? 为什么 Jacoco 不在代码覆盖率中包括 XQuery runner class,即使有多个单元测试? - Why does Jacoco not include an XQuery runner class in code coverage, even with multiple unit tests? 为什么Visual Studio在启用时会说“代码覆盖率未启用此测试运行”? - Why does Visual Studio say “Code Coverage is not enabled for this test run” when it's enabled? 为什么代码覆盖率功能区在源代码编辑器上不可见? - Why Code coverage ribbon is not visible on Source editor? 代码覆盖范围未达到类声明 - Code coverage does not reach class declaration MS 测试不会提高 sonarqube 中的代码覆盖率 - MS test does not improve code coverage in sonarqube 为什么右括号显示没有代码覆盖? - Why is a closing brace showing no code coverage? 为什么 PHPUnit + pcov 不生成代码覆盖率统计信息? - Why is PHPUnit + pcov not generating code coverage stats? 为什么nosetests说--with-coverage不是一个选择? - Why does nosetests say --with-coverage is not an option?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM