RE: MoveNext() method not covered by unit testing?



Hi Hongye,

Thanks for your reply! I'm glad that other people have experienced the same
problem. I did have a look on Google, but couldn't find any solutions.

Your second solution is not really viable, as we run everything from the
IDE. I’d rather not have to wait until 2010 either! ;)

So, the first option it is! I decided to write a test solution to prove the
concept. Here is the class I’m testing:

public class Items : IEnumerable<int>
{
private int[] items = new int[] { 1, 2, 3, 4 };

#region IEnumerable<int> Members

public IEnumerator<int> GetEnumerator()
{
foreach (int item in items)
{
yield return item;
}
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion
}

…and here is the unit test:

[TestMethod()]
public void ItemsConstructorTest()
{
Items t1 = new Items();

foreach (int i in t1)
{
System.Console.WriteLine(i);
}

IEnumerator<int> e1 = t1.GetEnumerator();

while (e1.MoveNext())
{
System.Console.WriteLine(e1.Current);
}

e1.MoveNext();

// =======================

IEnumerable t2 = new Items();

IEnumerator e2 = t2.GetEnumerator();

while (e2.MoveNext())
{
System.Console.WriteLine(e2.Current);
}

e2.MoveNext();
}

…as you can see, I’ve opted to write just one test to try and get 100%
coverage. In the “real world”, this would be a number of separate tests.

I believe I have now followed the advice in option 1. However, this still
isn’t giving me 100% test coverage. I’m getting the following entry in the
coverage:

Items.<GetEnumerator>d__0 2 13.33 % 13
86.67 %
MoveNext() 2 13.33 % 13 86.67 %

The first line is the summary, and the second the detail. Option 1 has given
slightly better coverage, but there still seems to be two elusive lines of
code! Could you suggest how to edit my tests to resolve this?

Many thanks,

Steve.

""Hongye Sun [MSFT]"" wrote:

Dear Steve,

Thanks for your post.

This is a known issue. It is reported at
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedba
ckID=329830.
It will be fixed in the upcoming beta of Visual Studio 2010.

In order to workaround the issue, you have two options:

1. Writing test code to cover the error logic code in MoveNext
As Marc Popkin-Paine said in the connect website, one way to workaround the
issue is rewrite the foreach as a while and move through the iterator
manually. This way can run the code blocks correspond to the error state
when you try to move the iterator beyond the final state. In order to
verify it, I wrote the following test code and it works well:

-----------------------------------
[TestMethod()]
public void GetEnumeratorTest()
{
Enumerator e = new Enumerator();
IEnumerator<int> er = e.GetEnumerator();
while (er.MoveNext())
{
Console.WriteLine(er.Current);
}
// The following line force the error state code executing
er.MoveNext();
}
-----------------------------------

2. Exclude compiler generated code from code coverage report
The only way, currently, to filter functions from coverage is to not
instrument them in the first place. Check out vsinstr's /exclude
command-line option. You can /exclude functions that you don't want to
show up in your coverage numbers. Here is the link:
http://msdn.microsoft.com/en-us/library/ms182402.aspx

Use the following name pattern to exclude:
namespace.MyClass`2.<GetEnumerator>d__0.*

This method has a limitation that you must generate code coverage report
through command line.

Please let me know if both methods work for you. Or you can wait for the
next beta of VS 2010 for the fix. Thanks.

Have a nice day.

Regards,
Hongye Sun (hongyes@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within?2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.








.



Relevant Pages

  • RE: Delegates/events
    ... public void OnMyEvent ... public class Second ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Code Coverage and the Auto-Build
    ... you must configure the testrunconfig to enable code coverage. ... Microsoft Online Community Support ... nature are best handled working with a dedicated Microsoft Support Engineer ...
    (microsoft.public.vsnet.enterprise.tools)
  • RE: problem with httpmodule
    ... Are you using IIS 6 or any newer version of IIS? ... Microsoft MSDN Online Support Lead ... nature are best handled working with a dedicated Microsoft Support Engineer ... public void Init ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Linking Mixed Mode and Managed Assemblies
    ... public void f ... Microsoft Online Community Support ... You can send feedback directly to my manager at: ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vc)
  • Re: OT Is anyone really surprised?
    ... In the early '60s, Mugabe joined Rhodesia's black ... local support and coverage in the college/independent press here. ... government as racist and oppressive as hell (people knew that they drafted about ...
    (rec.bicycles.racing)

Loading