How to get 100% code coverage for a "catch with rethrow" block in VB.NET in Visual Studio 2010 Ultimate?

I cannot achieve 100% code coverage for the "catch with rethrow" block in VB.NET source code. My workplace IDE is Visual Studio 2010 Ultimate. The following is an example of a simplified version of my real problem.

Source in C #: (a light blue background indicates full code coverage)

Source in C #


Equivalent source in VB.NET: (yellow background indicates partial code coverage)

Source in VB.NET


MSTests for C # and VB.NET source (designed to cover 100% of the code)

MSTests


Code Coverage Report

Code Coverage Report


The code coverage report shows 100% for C #, but only 91.67% for VB.NET. It also shows 1 block of code in which 0 lines are not found in VB.NET.

Is this a problem with the tool? Or am I missing something obvious?

EDIT # 1: Sharing source code on @Raptor request

C # source code

public class CodeCoverage { public void DoWork(bool flag = false) { try { Thread.Sleep(1); if (flag) { throw new Exception("test"); } } catch (Exception ex) { throw new Exception(string.Format("something happened: {0}", ex.Message)); } } } 


Source Code in VB.NET

 Public Class CodeCoverage2 Public Sub DoWork(Optional ByVal flag As Boolean = False) Try Thread.Sleep(1) If flag Then Throw New Exception("test") End If Catch ex As Exception Throw New Exception(String.Format("something happened: {0}", ex.Message)) End Try End Sub End Class 


Source Code for MSTests

 [TestClass] public class CodeCoverageTest { [TestMethod] public void DoWorkTest() { var obj = new CodeCoverage(); obj.DoWork(); } [TestMethod] [ExpectedException(typeof(Exception))] public void DoWorkTest2() { var obj = new CodeCoverage(); obj.DoWork(true); } [TestMethod] public void DoWorkTest3() { var obj = new CodeCoverage2(); obj.DoWork(); } [TestMethod] [ExpectedException(typeof(Exception))] public void DoWorkTest4() { var obj = new CodeCoverage2(); obj.DoWork(true); } } 
+6
source share
2 answers

If you look at the IL generated for the VB project in debug mode, in the catch block you will see the following:

 IL_002f: call string [mscorlib]System.String::Format(string, object) IL_0034: newobj instance void [mscorlib]System.Exception::.ctor(string) IL_0039: throw IL_003a: call void [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.ProjectData::ClearProjectError() IL_003f: leave.s IL_0041 

Since IL_0039 throws, you would never hit IL_003a , so you have code that will never be executed.

In release mode, IL for ClearProjectError is not generated.

+3
source

Not enough reputation to comment on John Kerner's answer, which is correct, but add - we decided this by conducting a test test in release mode. This causes the code coverage to work correctly.

EG:

 DEBUG Configuration: MyApplication - RELEASE ANY CPU MyTests - DEBUG ANY CPU 

Results:

 Old: DEBUG/DEBUG - 1 line missed N-1/N % coverage New: RELEASE/DEBUG - 0 lines missed, 100 % coverage 

Thank you John for pointing us in the right direction to look at the IL.

+1
source

Source: https://habr.com/ru/post/980511/


All Articles