OpenCover / opencover

A code coverage tool for .NET 2 and above (WINDOWS OS only), support for 32 and 64 processes with both branch and sequence points
https://blog.many-monkeys.com
Other
1.31k stars 247 forks source link

Adding tests that use Smocks interferes with branch points #364

Closed aburgett87 closed 8 years ago

aburgett87 commented 9 years ago

Using Smocks (https://github.com/vanderkleij/Smocks) seems to distort the calculation of branch points. If I add one test that uses Smocks all branch points in all tests (except for the test using Smock) increase by 2, adding 2 tests increase the branch points by a further 2, as far as I can see branch_points = branch_points + 2 * amount_smock_tests.

Any ideas on why this may happen?

ddur commented 9 years ago

If you use SharpDevelop, you can try to see what is going on, rihgt-click on method, choose ILSpy.

aburgett87 commented 9 years ago

On the assembly under test there isn't any difference in the IL if Smocks is used or not. It appears to be something with the tests themselves. The branch points go back to normal if I ignore the test fixture containing Smocks.

I should also report that I'm using, Moq (4.2.1507.118), NUnint (2.6.4.14350) and Smocks (0.5.22.0)

ddur commented 9 years ago

I guess any Mock or Smock has potential to create additional branches, Maybe you can filter it out?

aburgett87 commented 9 years ago

I thought so as well, but it effects every test, not just the test using Smock.

e.g. If I have a test suite including 4 smocks tests, the following will generate 10 branch points,

//Code under test example
if(tempString.Equals("Hello")) //Ten branch points here instead of two
{
   DoSomething()
}

When you say filter it out, do you mean the branches created or the tests themselves?

ddur commented 9 years ago

If assembly under test is unchanged, then new branches are created somewhere else. Usually I do filter out everything but coverage of system/assembly under test. Do you really need coverage of your test suite?

aburgett87 commented 9 years ago

I've just done a bit of research into Smocks and it turns out that it rewrites and then loads the new assemblies to use the faked modules. This seems to add to the list of modules that NUnit exposes and OpenCover picks up and processes it, because it has the same ModuleName as the assembly under test. I probably could find a way of filtering out the re-written assemblies, they all seem to go to the system's temp directory.

I don't really need to cover those tests, I was just trying to be a perfectionist and get 100% coverage haha. It really was nothing more than an experiment.

Would adding a new switch to the command line to skip modules with same name be something of interest? I'm more than happy to do it.

ddur commented 9 years ago

Why not? If it passes tests and doesn't break anything else .... Did you try to select only namespaces of your assembly under test?

ddur commented 8 years ago

@bogan87 If I understood you correctly, both original and by-smocks-rewritten assemblies are loaded into memory?

aburgett87 commented 8 years ago

Yep, that's exactly it.

ddur commented 8 years ago

@bogan87 Good, you can use one of installers from latest build and try exclusion filter like -<path to smock assemblies>[*]*

In example -<C:\project\smocks\bin\Debug\*>[*]*

Let us know if that helps

ddur commented 8 years ago

If profiler checks filter for process that is only .exe, full path process filter will not help here. We need to add full path assembly filter..

sawilde commented 8 years ago

https://www.nuget.org/packages/OpenCover/4.6.515-rc

sawilde commented 8 years ago

The excluded paths feature

We have published the release at https://www.nuget.org/packages/OpenCover/4.6.519 and https://github.com/OpenCover/opencover/releases/tag/4.6.519

NaveenRathan commented 7 years ago

Hi all, I am new to Smocks and OpenCover. Right now this issue is bothering me as I wont get 100% branch coverage. Can someone help me in providing example syntax how to exclude Smocks assemblies for code coverage. Below is what I have tried.

"C:\Users\uname\Documents\Visual Studio 2015\Projects\firstproject\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe" -register:Path32 "-target:MSTest.exe" -targetargs:"/testcontainer:firstprojectTests1.dll" -excludebyfile:<"C:\Users\uname\Documents\Visual Studio 2015\Projects\firstproject\firstprojectTests1\bin\Debug\Smocks.dll">[]

I have pointed to Smocks.dll as I did-not see where Smocks assemblies are created. Any help is appreciated.

sawilde commented 7 years ago

@unittester57 please open a new issue as I am not going to open a related but-closed issue.

NaveenRathan commented 7 years ago

@aburgett87 Were you able to resolve this issue at last? If so can you share sample script to exclude one of the assemblies and achieve complete branch coverage.