Teoman shipahi
Teoman shipahi

Reputation: 23042

NCrunch Unit testing code coverage

In my Visual Studio 2013 projects I am getting uncovered unit test warning from NCrunch where those functions have strong dependency to other entities. I would like to learn is there any natural way to clear out those warnings.

Lets assume I have a person class like this;

public class PersonApiHandler{    
public virtual Person GetPersonFromFacebook(){
// heavy facebook api call here
return person;    }
}

and I have a fake class like;

public class PersonApiHandlerFake: PersonApiHandler
{
public override Person GetPersonFromFacebook(){
// fake person is returning - isolated
return fakeUser;
}
}

then I am calling this method like;

    [TestFixture]
        public class PersonApiHandlerTests(){
    [Test]
    public void GetPerson_from_Api_success(){
    PersonApiHandlerFake fake = new PersonApiHandlerFake();
var fakeFacebookUser = fake.GetPersonFromFacebook();
Assert.IsNotNull(fakeFacebookUser );
    }

in this case NCrunch complains about PersonApiHandler.GetPersonFromFacebook is not code covered. Since this function calls real API, I cannot use it in my unit tests. Only way I found out using [ExcludeFromCodeCoverage] attribute to disable this warning. Is it right way to do or is there any other way to clear out uncovered code messages?

Upvotes: 2

Views: 715

Answers (1)

Prutswonder
Prutswonder

Reputation: 10064

NCrunch supports the use of specific inline comments to select one or more lines of code it should ignore from coverage. You can use //ncrunch: no coverage start and //ncrunch: no coverage end to mark the beginning and end of the code block, or use //ncrunch: no coverage to ignore a single line of code.

Upvotes: 2

Related Questions