locked
Code Coverage not covering the tests themselves? RRS feed

  • Question

  • I must be missing something simple, but I turned on Analyze Code Coverage in my new, Test Driven C# project. It's saying I don't have 100% coverage because of the unit tests themselves. For example, on the test:
            [TestMethod]
            [ExpectedException(typeof(ArgumentNullException))]
            public void NullConstructionTest()
            {
                new Foo(null);
            }// <- It's saying I don't have test coverage on this line...
    This test passes and works as expected. I don't understand what the Code Coverage Analysis wants here.
    Friday, November 9, 2012 7:17 AM

Answers

  • Hello Keratin,

    Thank you for posting in the MSDN forum.

    We can use the code coverage feature of Visual Studio to determine what proportion of your project's code is actually being tested by coded tests such as unit tests. If a line is not covered, it indicates the line was not exercised in the test run. Not all lines in a method will be covered.

    I have tried a test method with an assert.Equal(3,4) and define an AssertFailedException as ExpectedException. After run, I found the last line”}” is not covered either. If I try with an assert.Equal(4,4), the line”}” will be covered.  I feel that the test run only executes till assert line and new Fool(null) because of the exception throwing. So the line”}” is not exercised. You can try with non-null argument in the new Fool() to check if the line”}” will be covered.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Amanda Zhu Thursday, November 15, 2012 12:23 AM
    Monday, November 12, 2012 3:26 AM

All replies

  • Hello Keratin,

    Thank you for posting in the MSDN forum.

    We can use the code coverage feature of Visual Studio to determine what proportion of your project's code is actually being tested by coded tests such as unit tests. If a line is not covered, it indicates the line was not exercised in the test run. Not all lines in a method will be covered.

    I have tried a test method with an assert.Equal(3,4) and define an AssertFailedException as ExpectedException. After run, I found the last line”}” is not covered either. If I try with an assert.Equal(4,4), the line”}” will be covered.  I feel that the test run only executes till assert line and new Fool(null) because of the exception throwing. So the line”}” is not exercised. You can try with non-null argument in the new Fool() to check if the line”}” will be covered.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Amanda Zhu Thursday, November 15, 2012 12:23 AM
    Monday, November 12, 2012 3:26 AM
  • Hello Keratin,

    Any update? Could you get useful information from our reply?

    Would you mind letting us know the result of the suggestion?

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, November 13, 2012 9:10 AM
  • Hi Amanda,

    I feel like you correctly restated the OPs observations, but didn't provide a solution. This definitely seems like a bug in Visual Studio code coverage. We run into this frequently. I understand what's happening, but the expected results are that the test should show 100% code coverage assuming the last line of the method throws the expected exception. But that's not what happens.

    Is this issue entered into the VS backlog?

    Thanks,

    Paul

    Monday, February 10, 2014 10:36 PM
  • This is still an issue as of VS2017
    Thursday, August 9, 2018 2:12 PM
  • I've gotten around this by a rudimentary, but stupid method.

     [ExcludeFromCodeCoverage]
            public static Type GetThrownException(Action action)
            {
                try
                {
                    action();
                }
                catch (Exception ex)
                {
                    return ex.GetType();
                }

                Assert.Fail("There was not exception thrown ");
                return null;
            }

      [TestMethod]
            public void Test()
            {
                var t = ExpectException.GetThrownException(RunTest);
                Assert.AreEqual(t, typeof(InvalidOperationException));
            }

            [ExcludeFromCodeCoverage]
            private void RunTest()
            {
                Class1 c = new Class1();
                c.RunMethodThatThrowsException();

            }

    Friday, August 10, 2018 9:03 PM