简体   繁体   English

我可以从代码覆盖中排除部分方法吗?

[英]Can I exclude part of a method from code coverage?

I suspect the answer is no, but I'll ask anyway... 我怀疑答案是否定的,但无论如何我都会问...

TL;DR TL; DR

I know I can exclude a class or method from coverage analysis with the [ExcludeFromCodeCoverage] attribute, but is there a way to exclude only part of a method? 我知道我可以使用[ExcludeFromCodeCoverage]属性从覆盖率分析中排除类或方法,但有没有办法只排除方法的一部分

Concrete example 具体例子

I have a method that lazily generates a sequence of int.MaxValue elements: 我有一个方法,懒洋洋地生成一系列int.MaxValue元素:

private static IEnumerable<TElement> GenerateIterator<TElement>(Func<int, TElement> generator)
{
    for (int i = 0; i < int.MaxValue; i++)
    {
        yield return generator(i);
    }
}

In practice, it's never fully enumerated, so the end of the method is never reached. 在实践中,它永远不会被完全枚举,因此永远不会达到方法的结束。 Because of that, DotCover considers that 20% of the method is not covered, and it highlights the closing brace as uncovered (which corresponds to return false in the generated MoveNext method). 因此,DotCover认为该方法的20%未被覆盖,并且它突出显示右大括号(对应于在生成的MoveNext方法中return false )。

I could write a test that consumes the whole sequence, but it takes a very long time to run, especially with coverage enabled. 我可以编写一个消耗整个序列的测试,但是运行需要很长时间,尤其是启用了覆盖范围。

So I'd like to find a way to tell DotCover that the very last instruction doesn't need to be covered. 所以我想找一种告诉DotCover的方法,不需要覆盖最后一条指令。

Note: I know I don't really need to have all the code covered by unit tests; 注意:我知道我并不需要单元测试所涵盖的所有代码; some pieces of code can't or don't need to be tested, and I usually exclude those with the [ExcludeFromCodeCoverage] attribute. 某些代码段不能或不需要进行测试,我通常会排除那些带有[ExcludeFromCodeCoverage]属性的代码。 But I like to have 100% reported coverage for the code that I do test, because it makes it easier to spot untested parts of the code. 但我喜欢100%报告我测试的代码的覆盖率,因为它可以更容易地发现未经测试的代码部分。 Having a method with 80% coverage when you know there is nothing more to test in it is quite annoying... 当你知道没有更多东西需要测试时,有80%覆盖率的方法是非常烦人的...

First and foremost, while "code coverage" can be an important metric, one must realize that it just might not be possible to have 100% "code coverage". 首先,虽然“代码覆盖率”可能是一个重要的指标,但必须意识到它可能无法实现100%的“代码覆盖率”。 100% Code coverage is one of those metrics that you should aspire to attain, but which you never will; 100%代码覆盖率是您应该渴望实现的指标之一,但您永远不会这样做; ie get as close as you possibly can. 即尽可能接近。

OTOH, don't go crazy trying to get 100% code coverage. OTOH,不要试图获得100%的代码覆盖率。 More importantly, is your code readable? 更重要的是,您的代码是否可读? Is it testable (I presume so since you're looking at code coverage)? 它是可测试的(我假设您正在查看代码覆盖率)吗? Is it maintainable? 它可维护吗? Is it SOLID? 它是SOLID吗? Do you have passing unit, integration, and end-to-end tests? 您是否通过了单元,集成和端到端测试? These things are more important than achieving 100% code coverage. 这些比实现100%代码覆盖更重要。 What code coverage will tell you is how extensive your testing is (I'm not sure if the built-in code coverage analysis engine includes only unit tests, or includes all types of tests when calculating its statistics), which gives you an indication of whether or not you have enough tests. 代码覆盖率会告诉您测试的广泛程度(我不确定内置代码覆盖率分析引擎是否仅包含单元测试,还是在计算统计数据时包含所有类型的测试),这样可以指示你是否有足够的考试。 Also, while it will tell you how extensive your tests are (ie how many lines of code are executed by your tests), it won't tell you if your tests are any good (ie are your tests really testing what needs to be tested to ensure your application is working correctly). 此外,虽然它会告诉您测试的范围有多广(即测试执行了多少行代码),但它不会告诉您测试是否有任何好处(即您的测试是否真正测试了需要测试的内容)确保您的应用程序正常工作)。

Anyway, this may be not an answer, but food for thought. 无论如何,这可能不是一个答案,而是值得深思。

No, there is no way to exclude "part of a method" from coverage analysis with dotCover. 不,没有办法用dotCover从覆盖率分析中排除“方法的一部分”。

In the general sense you got a couple of options: 一般来说,你有几个选择:

  1. Extract the uncovered part into its own method, so you can properly ignore that method from analsysis 将未覆盖的部分提取到自己的方法中,这样您就可以从analsysis中正确地忽略该方法
  2. Ignore the problem 忽略这个问题

In this case there may be a third options. 在这种情况下,可能有第三种选择。 Since your test code exercises the majority of your method, perhaps you should just write a test method that makes sure the code runs to completion? 由于您的测试代码会运用您的大部分方法,或许您应该编写一个测试方法来确保代码运行完成?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM