JustMock & Mincrosoft Code Coverage

1 Answer 47 Views
Code Coverage JustMock Free Edition
Keenan
Top achievements
Rank 1
Keenan asked on 02 Feb 2025, 10:28 PM

Hey, 

I'm using the method Ivo created here https://www.telerik.com/forums/vs2022---justmock-throwelevatedmockingexception-with-net-6

to build my runsettings file and generate code coverage in the command line with .net8. 

Using 

dotnet test my.sln --configuration Release --no-build --no-restore --settings test-coverage.runsettings

I would like to exclude dlls with Test in the name and also output xml or cobertura if possible but I can't seem to get it to work. Any ideas? Run settings below

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <RunConfiguration>
    <ResultsDirectory>\CodeCoverageTestResults</ResultsDirectory>
    <TestAdaptersPaths>C:\JustMock\Libraries\DataCollectors;C:\Users\GitHubRunner\.nuget\packages\microsoft.codecoverage\17.11.0\build\netstandard2.0</TestAdaptersPaths>
  </RunConfiguration>

  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage Wrapper">
        <Configuration>
          <JustMockSettings>
            <ProfilerPath32>C:\JustMock\Libraries\32\Telerik.CodeWeaver.Profiler.dll</ProfilerPath32>
            <ProfilerPath64>C:\JustMock\Libraries\64\Telerik.CodeWeaver.Profiler.dll</ProfilerPath64>
          </JustMockSettings>
          <SinkDataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" isLinked="true">
            <!-- Exclude DLLs with ".tests." in the name -->
            <CodeCoverage>
              <Exclude>
                <ModuleFilePattern>*\.Tests\.*</ModuleFilePattern>
              </Exclude>
            </CodeCoverage>
          </SinkDataCollector>
        </Configuration>
      </DataCollector>
      <DataCollector friendlyName="JustMockDataCollector" />
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivo
Telerik team
answered on 03 Feb 2025, 03:46 PM

Hello Keenan,

I hope you could be able to achieve all of the desired functionality by following the Code Coverage Customization article on Microsoft docs, the paragraphs are: 'Include or exclude assemblies and members' and 'Code coverage formats'.

Regards,
Ivo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Keenan
Top achievements
Rank 1
commented on 03 Feb 2025, 05:25 PM

I've tried those in various sections, the wrapper, the JustMockDataCollector and the Code Coverage but I can't get anything to take. I'm not sure if I'm just putting those in the wrong section or something. I've also tried using command line arguments but they're still ignored. 
Ivo
Telerik team
commented on 04 Feb 2025, 08:55 AM

Here is a sample (pay attention to Format and ModulePaths nodes), you need to use the same configuration structure as DataCollector but under SinkDataCollector node
Keenan
Top achievements
Rank 1
commented on 04 Feb 2025, 05:25 PM

Here's my new runsettings file, it's still including Tests and outputting .coverage

  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage Wrapper">
        <Configuration>
          <JustMockSettings>
            <ProfilerPath32>C:\JustMock\Libraries\32\Telerik.CodeWeaver.Profiler.dll</ProfilerPath32>
            <ProfilerPath64>C:\JustMock\Libraries\64\Telerik.CodeWeaver.Profiler.dll</ProfilerPath64>
          </JustMockSettings>
          <SinkDataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" isLinked="true">
			<Configuration>
				<CodeCoverage>
					<Format>Cobertura</Format>
					<ModulePaths>
					  <Exclude>
						<ModulePath>.*Tests.*</ModulePath>
					  </Exclude>
					</ModulePaths>
				</CodeCoverage>
			</Configuration>
          </SinkDataCollector>
        </Configuration>
      </DataCollector>
      <DataCollector friendlyName="JustMockDataCollector" />
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Ivo
Telerik team
commented on 05 Feb 2025, 12:41 PM

Keenan, the last sample looks fine to me. I played around with it a bit and confirmed that Code Coverage does not respect the configuration, even when it is used directly without the JustMock Code Coverage Wrapper data collector. This leads me to believe there may be a general issue. I recommend trying to get it to work alone first (in this case you need to use a do-nothing unit test) and then reusing the working configuration.
Keenan
Top achievements
Rank 1
commented on 05 Feb 2025, 05:54 PM

Hey Ivo, 

Managed to get it working, (my module path wasn't right) but could not get cobertura output. When it was added the output threw

Telerik.JustMock.Core.ElevatedMockingException: Cannot mock 'ClassName'. The profiler must be enabled to mock, arrange or execute the specified target.
      Detected active third-party profilers:
      * C:\Users\user\.nuget\packages\microsoft.codecoverage\16.7.1\build\netstandard1.0\CodeCoverage\covrun32.d
      ll (from process environment)
      Disable the profilers or link them from the JustMock configuration utility. Restart the test runner and, if
      necessary, Visual Studio after linking.
While cobertura output isn't required, I thought I'd point this out for anyone else. 
Ivo
Telerik team
commented on 06 Feb 2025, 07:59 AM

The exception is expected since the wrapper collector allows JustMock and Code Coverage (the file path appears in the exception message) profilers to coexist. Nonetheless, it is great that you have managed to get it working as you like it.
Ivo
Telerik team
commented on 06 Feb 2025, 09:50 AM

Keenan, found a way to change the report format: simply move the setting one node up under Configuration, and it works. The documentation appears to be incorrect.
Keenan
Top achievements
Rank 1
commented on 06 Feb 2025, 06:15 PM

It runs but still outputs .coverage files. Not sure why but it's not a blocker. 
Ivo
Telerik team
commented on 07 Feb 2025, 12:12 PM

I am using the latest Code Coverage version 17.12.0, probably it matters. Anyway, glad to hear that everything works as expected.
Tags
Code Coverage JustMock Free Edition
Asked by
Keenan
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or