When you use JustMock elevated mocking in your tests you need to enable the profiler on your build machine. To enable the profiler you have to set 3 environment variables.

• JUSTMOCK_INSTANCE = 1
• COR_ENABLE_PROFILING = 1
• COR_PROFILER = {B7ABE522-A68F-44F2-925B-81E7488E9EC0}

Basically, when the CLR gets initialized it checks for these variables and if they are set it attaches the profiler, specified by the COR_PROFILER guid.

To make things easier we have implemented MsBuild tasks - JustMockStart and JustMockStop. You can read more about it here: JustMock MsBuild tasks.

These tasks just set and unset the environment variables. The problem is that they expect you to run the tests with the Exec task which starts a new process that picks up the environment variables already set with the JustMockStart task.

On the other hand, the MsBuild runner provided by xUnit runs the tests directly inside the MsBuild process. The JustMock tasks set the environment variables, but there is no new test executor process to attach the profiler to.

Here is a sample MsBuild project taken from xUnit documentation:

<Project
DefaultTargets="Test"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

AssemblyFile="path\to\xunit.runner.msbuild.dll"

<Target Name="Test">
<xunit Assemblies="path\to\MyTests.dll" />
</Target>

</Project>

To make that work you need to have the environment variables set when the MsBuild process starts. How to achieve that really depends on your build infrastructure. You could:

1. Set the environment variables directly on the build machine.
2. Set the build server to arrange the environment variables of your build/job.
3. Set the environment variables at the start of your build script.

The first option is not recommended. You should attach the profiler only when you need it, i.e. when you run your tests. Leaving the variables on the machine will attach the profiler to every .NET process.

The second approach is fine, although it binds your build process to a specific build server.

I prefer the third way. We use Powershell scripts for all of our builds. We specify the environment at the beginning of the script and then just call MsBuild.exe with the arguments. Having that done, you can easily integrate with every build server as running the build is just executing a script.

There is one more issue to tackle caused by the fact that the MsBuild xUnit runner executes the tests directly in the running assembly (MsBuild.exe).

JustMock has a configurable process blacklist for processes that JustMock profiler shouldn’t attach to. The idea is to blacklist certain processes so we don’t hurt the performance when you have the profiler enabled. MsBuild.exe is in that list by default. To make JustMock elevated mocking work with xUnit MsBuild runner you need to remove MsBuild.exe from that blacklist. You can find the blacklist in one of the following

• C:\Program Files (x86)\Telerik\JustMock\Libraries\CodeWeaver\32\ImageBlacklist.cfg
• C:\Program Files (x86)\Telerik\JustMock\Libraries\CodeWeaver\64\ImageBlacklist.cfg

depending on the MsBuild process, whether it is 32bit or 64bit.