Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

What to do with dependent uncovered functions?
reyou
#1 Posted : Wednesday, January 14, 2015 1:14:16 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/14/2015(UTC)
Posts: 3

Same question can be found at: http://stackoverflow.com/questions/27934297/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?
Remco
#2 Posted : Wednesday, January 14, 2015 3:49:06 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Hi, thanks for posting!

NCrunch itself doesn't give warnings or messages about uncovered code. The only visible indication you'll see of this is the colour of the markers next to the code - which isn't really considered to be an error or a warning unless you treat it as such.

If for some reason you don't want to see the uncovered markers (for example, if you have organisational policies around code coverage metrics), the best way to remove them from view is to use NCrunch's code coverage suppression comments.

NCrunch does support the ExcludeFromCodeCoverage attribute as used by other testing tools, but I generally recommend using the suppression comments instead as they don't leave artifacts in your compiled code and are generally more reliable when dealing with code blocks.


Cheers,

Remco
reyou
#3 Posted : Wednesday, January 14, 2015 2:38:59 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/14/2015(UTC)
Posts: 3

Hi thanks for your reply.

Yes I was referring to black dots indicates code is not covered. But I wanna see all of them green to feel myself comfortable! I do not wanna suppress them until I have to.

So if my function has dependency to 3rd party APIs and cannot be called from unit test what is the best way to tell NCrunch that it is covered?

Should I create integration test project and call this function and turn the lights to green? Or just use suppression comments to tell NCrunch to not to check this function for code coverage?

Thanks for your answer.
Remco
#4 Posted : Wednesday, January 14, 2015 10:15:53 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
reyou;6778 wrote:

So if my function has dependency to 3rd party APIs and cannot be called from unit test what is the best way to tell NCrunch that it is covered?

Should I create integration test project and call this function and turn the lights to green? Or just use suppression comments to tell NCrunch to not to check this function for code coverage?


The answer to this is quite subjective, as I guess it depends upon how you choose to use the tool and what your personal goals in doing so are.

I personally try to avoid suggesting to people to chase code coverage itself, as metrics such as this tend to distract from the core objective of building quality working software, and they can also create a false sense of security about how heavily code is tested (i.e. just because its covered, doesn't mean that its well tested). As a tool, one of NCrunch's goals is to inform you of the behaviour of your code under test. It aims to inform you rather than define any particular state as 'good' or 'bad', instead empowering you to decide for yourself.

For this reason, NCrunch doesn't contain any features that attempt to mark code as covered when there is no way it could be physically covered. Nor does it contain features that would enable you to misrepresent your own physical code coverage.

However, it does allow you to suppress coverage data where you feel this data is unnecessary or unwanted (through code coverage suppression comments).

But with the tool aside, I would suggest that finding a way to test this code may be worthwhile if you feel that there is the possibility it may fail. An integration test of some kind would be ideal for this. Although if you do choose to write an integration test to test this code, I recommend doing so with the objective of testing the code as it is expected to behave, rather than just trying to make the lights green :)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.051 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download