简体   繁体   中英

Why is Code Coverage not reporting 100% code coverage in test code when checking for exceptions?

Consider the following code:

public interface IConverter
{
}

public class ConverterFactory
{
    public IConverter GetConverter()
    {
        throw new ArgumentException();
    }
}

[TestClass]
public class ConverterFactoryTests
{
    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void GetConverterShouldThrowExceptionWhenConverterNotRegistered()
    {
        var factory = new ConverterFactory();
        factory.GetConverter();
    }
}

Why is code coverage reporting that the test method is not 100% covered?

ANSWER:

The closing curly bracket is not covered as the code will always throw an exception and never get to the end of the method.

How do I obtain 100% coverage when an exception is thrown in a unit test?

So it appears that to get 100% coverage you would need to exclude test methods that check exceptions. Annoying.

EDIT1: Removed fluent assertions as not relevant. EDIT2: Removed generics as not relevant.

The closing curly bracket is not covered as the code will always throw an exception and never get to the end of the method.

How do I obtain 100% coverage when an exception is thrown in a unit test?

So it appears that to get 100% coverage you would need to exclude test methods that check exceptions. Annoying.

将代码转换成如下所示,当然如果是一行。

public IConverter GetConverter() throw new ArgumentException();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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