故意失败测试的 Ncover 代码覆盖率结果

发布于 2024-09-11 00:54:04 字数 757 浏览 3 评论 0原文

我有几个单元测试辅助扩展方法,例如 IsNotEmpty(this string str),我还为其编写了单元测试。这些测试的基本结构是,我有一个测试验证扩展方法是否通过并在“好”输入上返回预期结果,还有一个测试验证在“坏”输入上抛出失败异常。例如,上述方法的失败测试可能如下所示:

[TestMethod, Fails]
public void IsNotEmpty_Fails_On_Empty_String()
{
    "".IsNotEmpty();
}

其中 Fails 是我自己编写的属性 - 基本上是 [ExpectedException(typeof(AssertFailedException))]。因此,我明确期望在上述测试中的唯一一行上抛出异常,因此,我期望该方法一路运行。

但是,当我右键单击我的解决方案,选择“Test with->NOver”时,我在与上述方法类似的所有方法上获得的代码覆盖率均低于 100%,并且末尾大括号突出显示为红色。

为什么 NCover 抱怨我的测试方法没有完成,而我已经说过它不会完成?

**)对于所有那些 100% 回复的人代码覆盖率不是必需的:不,我并不特别担心不惜一切代价维持 100% 的代码覆盖率。然而,在这些情况下,确实重要的方法显示为列表中未涵盖,我必须一直深入到树中才能看到只有它们。这就是问题所在我试图到达这里 - 一般来说不是 100% 的代码覆盖率。

I have a couple of unit test helper extension methods, such as, IsNotEmpty(this string str) for which I have also written unit tests. The basic structure of these tests is that I have one test that verifies that the extension method passes and returns the expected result on a "good" input, and one that verifies that a fail exception is thrown on "bad" input. For example, the fail tests for the above method might look like this:

[TestMethod, Fails]
public void IsNotEmpty_Fails_On_Empty_String()
{
    "".IsNotEmpty();
}

where Fails is an attribute I wrote myself - basically a proxy for [ExpectedException(typeof(AssertFailedException))]. So I'm explicitly expecting an exception to be thrown on the only line in the above test, and thus, I'm not expecting the method to run all the way.

But when I right-click my solution, select "Test with->NCover", I get less than 100% code coverage* on all methods like the above one, with the ending brace highlighted red.

Why does NCover complain about my test method not finishing, when I've said that it won't?

**) For all those of you who will reply that 100% code coverage is not necessary: No, I'm not particularily worried about maintaining a 100% code coverage at all costs. However, in these cases methods that* do matter show up as not covered in the list, and I have to go all the way into the tree to see that it's just them. That's the issue I'm trying to get at here - not 100% code coverage in general.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-09-18 00:54:04

您正在运行调试版本吗?这可能就是问题所在。 NCover 在调试断点的基础上工作,微软在每个方法的右括号中添加了一个断点,这样它就可以在离开该方法之前单步执行到那里。在发布模式下运行应该会为您处理这些右括号。

Are you running a debug build? That's probably the issue. NCover works off of the debugging breakpoints, and Microsoft adds one to the closing bracket of each method so it can step there before leaving the method. Running in release mode should take care of those closing brackets for you.

夕色琉璃 2024-09-18 00:54:04

不用担心 100% 的代码覆盖率。只要尽可能接近即可。

获得 100% 的代码覆盖率是浪费时间。

重要的是您对覆盖需要覆盖的代码的测试充满信心。

您可以拥有一组可靠的测试,仅覆盖 70% 的代码。

您最好的测试可能只涵盖几行,但它们可以防止大多数错误。它们是“边缘情况测试”。没有它们,其他测试就没用了。您的整个测试套件都是无用的,因为它们是测试错误的情况,如果这些错误不存在,这些错误通常会不断重新引入到代码库中。

Don't worry about 100% code coverage. Just get as close as you reasonably can.

Attaining 100% code coverage is a waste of time.

All that matters is that you are confident in your tests covering the code that needs to be covered.

You can have a solid set of tests that only cover 70% of your code.

Your best tests likely only cover a few lines, but they are the ones that will prevent most of the bugs. They are the 'edge case tests'. The ones that without them, the other tests are useless. Your entire test suite is useless, as they are the cases that test the mistakes that would typically keep getting reintroduced into code base if they didn't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文