Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage for async methods

When I analyse code coverage in Visual Studio 2012, any of the await lines in async methods are showing as not covered even though they are obviously executing since my tests are passing. The code coverage report says that the uncovered method is MoveNext, which is not present in my code (perhaps it's compiler-generated).

Is there a way to fix code coverage reporting for async methods?

Note:

I just ran coverage using NCover, and the coverage numbers make a lot more sense using that tool. As a workaround for now, I'll be switching to that.

like image 531
Jacob Avatar asked Mar 24 '13 20:03

Jacob


People also ask

What happens when you call async method?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

What should I exclude from code coverage?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

Can async method have out parameter?

Allowed Parameter Types in Async Methods One of the reasons that Task<T> is needed to pass back data to the caller is that async methods are not allowed to have ref or out parameters.


1 Answers

This can happen most commonly if the operation you're awaiting is completed before it's awaited.

I recommend you test at least synchronous and asynchronous success situations, but it's also a good idea to test synchronous and asynchronous errors and cancellations.

like image 122
Stephen Cleary Avatar answered Sep 28 '22 08:09

Stephen Cleary