5

I have some test methods of the form:

[ExpectedException(typeof(MyException))]
public void Method() {
  SUT.DoSomethingThatRaisesMyException();
}

in my test project. However when I turn on code coverage I have noticed that I am getting less than 100% coverage on the unit test project. This seems to be because the code coverage tool never sees the closing brace of Method() - it always exceptions before it gets there.

I can add the [ExcludeFromCodeCoverage] attribute to every test method, but that seems too clunky. Is there a way of excluding entire projects? Or is there a way of automatically excluding every test project.

Jonny
  • 2,509
  • 23
  • 42
  • 1
    You can add the attribute on clases, too. That's not a solution, but better than putting it on every method. – nvoigt Oct 04 '13 at 12:57
  • Why do you enable codecoverage for a project and then exclude the whole project? I does not make any sense to enable it in the first place except to trick your statistics. But you can't make good software driven by numbers if you fake your number. – Peter Oct 04 '13 at 13:11
  • 5
    @peer - Code coverage is enabled for the whole solution, I want to exclude individual projects such as test projects. Is there a flag in the project settings that you know about? – Jonny Oct 04 '13 at 13:23
  • 2
    With the latest Visual Studio version or any other editor you can edit your `Properties/AssemblyInfo.cs` and add two lines: `using System.Diagnostics.CodeAnalysis; [assembly: ExcludeFromCodeCoverage]` – Junior Mayhé Mar 27 '20 at 15:17
  • 1
    @JuniorMayhé Thanks for leaving a comment, you just made my day :). – DotBert Jun 03 '20 at 11:24

0 Answers0