Category Archives: 10536

Getting the Typemock TFS build activities to work on a TFS build agent running in interactive mode

Windows 8 store applications need to be built on a TFS build agent running in interactive mode if you wish to run any tests. So whilst rebuilding all our build systems I decided to try to have all the agents running interactive. As we tend to run one agent per VM this was not going to be a major issue I thought.

However, whilst testing we found that any of our builds that use the Typemock build activities failed when the build agent was running interactive, but work perfectly when it was running as a service. The error was

Exception Message: Access to the registry key ‘HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\TypeMock’ is denied. (type UnauthorizedAccessException)
Exception Stack Trace:    at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
   at Microsoft.Win32.RegistryKey.CreateSubKeyInternal(String subkey, RegistryKeyPermissionCheck permissionCheck, Object registrySecurityObj, RegistryOptions registryOptions)
   at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck)
   at Configuration.RegistryAccess.CreateSubKey(RegistryKey reg, String subkey)
   at TypeMock.Configuration.IsolatorRegistryManager.CreateTypemockKey()
   at TypeMock.Deploy.AutoDeployTypeMock.Deploy(String rootDirectory)
   at TypeMock.CLI.Common.TypeMockRegisterInfo.Execute()
   at TypeMock.CLI.Common.TypeMockRegisterInfo..ctor()   at System.Activities.Statements.Throw.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

 

So the issue was registry access. Irrespective of whether running interactive or as a service I used the same domain service account, which was a local admin on the build agent. The only thing that changed as the mode of running.

After some thought I focused on UAC being the problem, but disabling this did not seem to fix the issue. I was stuck or so I thought.

However, Robert Hancock unknown to me, was suffering a similar problem with a TFS build that included a post build event that was failing to xcopy a Biztalk custom functoid DLL to ‘Program Files’. He kept getting an ‘exit code 4 access denied’ error when the build agent was running interactive. Turns out the solution he found on Daniel Petri Blog also fixed my issues as they were both UAC/desktop interaction related.

The solution was to create a group policy for the build agent VMs that set the following

  • User Account Control: Behavior of the elevation prompt for administrators in Admin Approval Mode – Set its value to Elevate without prompting.
  • User Account Control: Detect application installations and prompt for elevation – Set its value to Disabled.
  • User Account Control: Only elevate UIAccess applications that are installed in secure locations – Set its value to Disabled.
  • User Account Control: Run all administrators in Admin Approval Mode – Set its value to Disabled.

Once this GPO was pushed out to the build agent VMs and they were rebooted my Typemock based build and Robert Biztalk builds all worked as expected

TF900548 when using my Typemock 2012 TFS custom build activity

Using the Typemock TFS 2012 Build activity I created I had started seen the error

TF900548: An error occurred publishing the Visual Studio test results. Details: ‘The following id must have a positive value: testRunId.’

I thought it might be down to having patched our build boxes to TFS 2012 Update 1, maybe it needed to be rebuild due to some dependency? However, on trying the build activity on my development TFS server I found it ran fine.

I made sure I had the same custom assemblies and Typemock autorun folder and build definition on both systems, I did, so it was not that.

Next I tried running the build but targeting an agent on on the same VM as the build controller. This worked, so it seems I have a build controller issues. So I ran Windows update to make sure the OS was patched it to date, it applied a few patches and rebooted. And all was OK my test ran gain.

It does seem that for many build issues the standard switch it off and back on again does the job

Cannot run Microsoft Fakes based test if Typemock Isolator enabled

With Microsoft Fakes moving to the Premium SKU of Visual Studio in 2012.2 (CTP4 is now available) more people will be looking at using them.

I have just installed CTP4 and have seen a behaviour I don’t think I have not seen in the previous version of Visual Studio (I need to check because as well as CTP4  I have recently installed the new version of Typemock Isolator 7.3.0 that addresses issues with Windows 8 and Visual  Studio 2012).

Anyway the error you see when you run a fakes based test is ‘UnitTestIsolation instrumentation failed to initialialize, Please restart Visual Studio and rerun this test’

image

The solution is to disable Typemock Isolator (Menu Typemock > Suspend Mocking), when this is done, without a reboot, the Fakes based test run.

Does mean you can’t have a solution using both Fakes and Isolator, but why would you?

For those hard to mock moments – Microsoft Fakes or Typemock Isolator?

About a year ago I wrote a post ‘Now that VS11 has a fake library do I still need Typemock Isolator to fake out SharePoint?’. Well this discussion becomes relevant for more people as with Visual Studio 2012.2 (currently available as a CTP) the Microsoft Fakes move from the Ultimate SKU to the Premium SKU.

From my experience the Ultimate SKU is not present on too many developer’s PCs. It is most commonly found on the PCs of the team leads, software architects or test developers (managing coded UI/load testing etc. efforts). If a team was historically going to use Microsoft Fakes then they had to buy more Ultimate SKUs; as what is the point of a unit test using a mocking framework that only part of the team can run?

The Premium SKU of Visual Studio is far more common, I would go as far as to say it is the corporate standard for development. Now as this SKU contains Test Manager (since 2012 RTM) it covers most jobs most developers do. Ultimate is just needed for the specialists in the team. Adding fakes to the Premium SKU really makes sense if Microsoft want to drive adoption.

So now the question of whether to use Microsoft Fakes or Typemock Isolator (or Telerik JustMock a product I have to admit I have not used in anger) is rebalanced as there is a fair chance a development team may all be licensed for Microsoft Fakes as they have the premium SKU. The question becomes is the cost of Isolator justified by the features it offers over and above Microsoft Fakes?

This is not an uncommon form of question for any third party add-in to Visual Studio. Visual Studio offers refactoring, but I think few would argue that Resharper or RefactorPro! don’t offer more features that justify their cost.

For me the big advantage of Typemock is ease of use and consistent syntax across all usage patterns. This could be just due to familiarity, but the fact I don’t need to manually generate the fake assembly is a bonus. Also that Isolator’s fluent API is basically the same as Moq and FakeItEasy so causes less friction when coming to advanced mocking from these tools. A team can use the free basic version of Typemock Isolator until they need the advanced features when they need to license it.

Fakes is a different way of working to most other frameworks, working at a different level inside Visual Studio. A disadvantage of this is that it does not lend itself well to refactoring, you are probably going to have to regenerate the fake assemblies after any refactor, which can be slow. Also this makes refactoring a bit more risky, as you also have to touch unit tests, a manual operation.

I think at this time for me Isolator still offers advanced features and easy of use advantages that justifies the license cost. However, as with all tools this is an ever changing field, I expect to see new features and changes for all the players in the fakes market as they all aim to better address the problems cause by the poorly architecture of applications/frameworks such as SharePoint and of course our own poorly designed legacy code.

New release of Typemock Isolator

Typemock have released a new version of Isolator (7.3) that addresses some problems they have been having with Visual Studio 2012 and Windows 8

  • Resolved all issues concerning the breaking MS update
  • Boosted performance across the board (up to 44% faster)
  • Enhanced SmartRunner UI
  • Improved mocking capabilities
  • Better integration with .NET development ecosystem

You can download this release, or a free trial if you don’t have an Isolator license, from the Typemock site

Release of Typemock Isolator Basic edition

Some great news today from Typemock, there is now a free basic edition of Typemock Isolator. The addresses a key historic problem with Isolator, that of its cost when you don’t need the advanced features of Isolator all the time.

Now if you need the cool advanced mocking features of Isolator, such as mocking sealed private classes, then the cost is not really a factor, you buy the product or don’t get the features. However what do you do if you just want to do just do ‘normal mocking’ in a project ? e.g. mock out an interfaces. Do you use Typemock as you already have it, or swap to a different mocking framework, only using Typemock when you have to use its advanced features?

This is a particular problem for consulting/bespoke development companies such as mine, we write code for clients that in the future they will have to maintain themselves, they are not that happy with us passing over code with a dependency on a licensed mocking framework unless it is essential to their project. This means in the past I have tended to use other mocking frameworks, usually FakeItEasy as its syntax is very similar to Typemock Isolator, unless I need its advanced features of Typemock such as in SharePoint projects.

However with this new basic edition release from Typemock this is no longer an issue. I can use Typemock in all my projects. If a client need to run the tests, as long as they are ‘normal mocking’ ones, all they need to do is install this new free version of Typemock and the project builds and the tests run. There is only a need to purchase a license if the advanced features of Typemock are required.

So longer do I need to swap mocking framework for only licensing reasons, hence reducing the friction I have had in the past changing mocking syntax.

Getting Typemock Isolator running within a TFS 2012 build – part 2

I posted previously on getting Typemock 7.x running in a TFS 2012 RC build process . Well it seems the activities I previously published did not work on the TFS 2012 RTM build i.e if you do nothing other than upgrade your TFS server from RC to RTM a previously working build fails, no attempt was made to run any tests and I got the unhelpful error

TF900546: An unexpected error occurred while running the RunTests activity: ‘Executor process exited.’.

Note: TF900546 seems to be the generic – test failed error number. If you see it you will usually have to look elsewhere for anything helpful.

So I assumed that the problem must be some difference with the TeamFoundation assemblies I was referencing between the RC and RTM versions, so I rebuilt my activities, all to no effect, I got the same error. So I did some more digging into the code. I found a number of issues, why these had not caused an issue before I don’t know:

Target property

If you do not specify a .NET version via the Target property of the TypeMockRegister activity it does not attempt to start interception. As setting this property every time you want to use the activity is a pain, I modified the activity so that if no Target property is passed then the value v4.0.30319 is used, the version of .NET 4.5 as it appears in the c:\windows\Microsoft.Net\framework folder.

Note Missing of the leading v if the Target value causes the TFS build agent to hang, I have no idea why.

Once this change was made the build ran and it tried to run all my tests, but the ones involving Typemock failed, with the message

Test method BuildProcessValidation.Tests.MSTestTypemockTests.DirtyTrickMockingWithTypemock_Email_is_sent_when_client_order_is_processed threw exception:
System.TypeInitializationException: The type initializer for ‘f5′ threw an exception. —> System.TypeInitializationException: The type initializer for ‘TypeMock.InterceptorsWrapper’ threw an exception. —> TypeMock.TypeMockException:
*** Typemock Isolator needs to be linked with Coverage Tool to run, to enable do one of the following:
   1. link the Coverage tool through the Typemock Isolator Configuration
   2. run tests via TMockRunner.exe -link
   3. use TypeMockStart tasks for MSBuild or NAnt with Link
For more information consult the documentation (see Code Coverage with Typemock Isolator topic)

On looking in the build box’s event log I saw the message

.NET Runtime version 4.0.30319.17929 – Loading profiler failed during CoCreateInstance.  Profiler CLSID: ‘{B146457E-9AED-4624-B1E5-968D274416EC}’.  HRESULT: 0x8007007e.  Process ID (decimal): 2068.  Message ID: [0x2504].

AutoDeployment

Basically the issue was the Typemock interceptor, the profiler, was not being started because Typemock was not installed on the build box. To prove this I manually installed Typemock on the build box and the error went away, all my tests ran. So happy my activity basically worked, I removed Typemock from the build box and the problem returned, so I know I had an autodeployment issue.

On checking the activity code again I found I was not handling the nullable boolean correctly for the AutoDeploy build argument of the type TypemockSettings. As soon as this was fixed and deployed by build leapt into life.

In summary

So I am please to say I have a working activity again, as I said in my previous post I see this as stopgap measure until Typemock Release their official version. This set of activities have had minimal testing and I am not sure the undeploy logic is working fully, but as I don’t need this feature I am not worrying about it for now.

Hope you find it useful in its current state.

Using an internal Nuget server to manage the Typemock assembly references.

In my last post I discussed the process I needed to go through to get Typemock Isolator running under TFS 2012. In this process I used the Auto Deploy feature of Isolator. However this raised the  question of how to manage the references within projects. You cannot just assume the Typemock assemblies are in the GAC, they are not on the build box using auto deploy. You could get all projects to reference the auto deployment location in source control. However, if you use build process templates across projects it might be you do not want to have production code referencing build tools in the build process are directly.

For most issues of this nature we now use Nuget. At Black Marble we make use of the public Nuget repository for tools such as XUnit, SpecFlow etc. but we also have an internal Nuget repository for our own cross project code libraries. This includes licensing modules, utility and data loggers etc.

It struck me after writing the last post that the best way to manage my Typemock references was with a Nuget package, obviously not a public one, this would be for Typemock to produce. So I create one to place on our internal Nuget server that just contained the two DLLs I needed to reference (I could include more but we usually only need the core and act assert arrange assemblies).

image

IT IS IMPORANT TO NOTE that using a Nuget package here in no way alters the Typemock licensing. Your developers still each need a license, they also need to install Typemock Isolator, to be able to run the tests and your build box needs to use auto deployment. All using Nuget means is that you are now managing references in the same way for Typemock as any other Nuget managed set of assemblies. You are internally consistent, which I like.

So in theory as new versions of Typemock are released I can update my internal Nuget package allowing projects to use the version they require. It will be interesting to see how well this works in practice.

Getting Typemock Isolator running within a TFS 2012 build

I have posted in the past about getting Typemock Isolator to function within the TFS build process. In TFS 2008 it was easy, you just ran a couple of MSBUILD tasks that started/stopped the Typemock Isolator inception process (the bit that does the magic other mocking frameworks cannot do). However with TFS 2010’s move to a windows workflow based build model it became more difficult. This was due to the parallel processing nature of the 2010 build process, running a single task to enable interception cannot be guaranteed to occur in the correct thread (or maybe even on the correct build agent). So I wrote wrapper build activity for MStest to get around this problem. Howerver, with the release of Typemock Isolator 6.2 direct support for TFS 2010 was added and these TFS build activities have been refined in later releases. In the current beta (7.0.8) you get a pre-created TFS build process template to get you going and some great auto deploy features, but more of that later.

The problem was I wanted to put Isolator based tests within a TFS 2012 build process. I posted before about my initial thoughts on the problem. The main problem is that TFS build activities have to be built against the correct version of the TFS API assemblies (this is the reason the community custom activities have two sets of DLLs in the release ZIP file). So out the box you can’t use the Typemock.TFS2010.DLL with TFS 2012 as it is built against the 2010 API.

Also you cannot just use the Typemock provided sample build process template. This is built against 2010 too, so full of 2010 activities which all fail.

What I tried that did not work (so don’t waste your time)

So I took a copy of the default TFS 2012 build process template and followed the process to add the Typemock.TFS2010.DLL containing the Typemock activities to the Visual Studio 2012 toolbox (the community activity documentation provides a good overview of this strangely complex process also see the ALM Rangers guidance). I then added the TypemockRegister and TypemockStart activities at the start of the testing block. For initial tests I did not both adding the TypemockStop activity

image

I then made sure that

  • Typemock was installed on the build agent PC
  • The Typemock.TFS2010.dll was in the correct CustomActivities folder in source control
  • The build controller was set to load activities from the CustomActivities folder.

However, when I tried to queue this build I got an error 

Exception Message: Object reference not set to an instance of an object. (type NullReferenceException)
Exception Stack Trace:    at TypeMock.CLI.Common.TypeMockRegisterInfo.Execute()

image 

The issue was that though Typemock was installed, the required DLLs could not be found. Checking in a bit more detailed (by running the build with diagnostic level of logging and using Fuslogvw) I saw it was trying load the wrong versions of DLLs as expected. So the first thing I tried to use was binding redirection (a technique I used before with similar Typemock). This in effect told the Typemock activity to use the 2012 DLLs when it asks for the 2010 ones. This is done by using an XML config file (Typemock.TFS2010.DLL.config)  in the same folder as the DLL file.

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="Microsoft.TeamFoundation.Build.Workflow"
                           publicKeyToken="b03f5f7f11d50a3a"
                           culture="neutral" />
         <bindingRedirect oldVersion="10.0.0.0"
                          newVersion="11.0.0.0"/>
       </dependentAssembly>
       <dependentAssembly>
         <assemblyIdentity name="Microsoft.TeamFoundation.Build.Client"
                           publicKeyToken="b03f5f7f11d50a3a"
                           culture="neutral" />
         <bindingRedirect oldVersion="10.0.0.0"
                          newVersion="11.0.0.0"/>
       </dependentAssembly>
          <publisherPolicy apply="no">
      </assemblyBinding>bu
   </runtime>
</configuration>

I first tried to add this file to the CustomActivities source control folder, where the custom activities are loaded from by the build agent, but that did not work. I could only get it to work if I put both the DLL and the config files in the  C:\Program Files\Microsoft team Foundation Server 1.0\Tools folder on the build agent. This is not a way I like to work, too messy having to fiddle with the build agent file system.

Once this setting was made I tried a build again and got the build process to load, but the TypemockRegister activity failed as the Typemock settings argument was not set. Strangely Typemock have chosen to pass in their parameters as a complex type (of the type TypemockSettings) as opposed to four strings. Also you would expect this argument to be passed directly into their custom activities by getting activity properties to argument values, but this is not how it is done. The Typemock activities know to look directly for an argument called Typemock. This does make adding the activities easier, but not obvious if you are not expecting it. So I added this argument to the build definition in Visual Studio 2012 and checked it in, but when I tried to set the argument value for a specific build it gave the error that the DLL containing the type Typemock.TFS2010.TypemockSettings could not be loaded, again the TFS 2010/2012 API issue, this time within Visual  Studio 2012

 

clip_image002

 

At this point I gave up on binding redirection, I had wasted a lot more time than this post makes it sound. So I removed all the work I had previously done and thought again.

What did work

I decided that the only sensible option was to recreate the functionality of the Typemock activity against the 2012 API. So I used Telerik JustDecompile to open up the Typemock.Tfs2010.dll assembly and had a look inside. In Visual Studio 2012 I then created a new C# class library project called Typemock.BM.TFS20102 targeting .NET 4. I then basically cut and pasted the classes read from JustDecompile into classes of the same name in the new project. I then added references to the TFS 2012 API assemblies and any other assemblies needed and compiled the project. The one class I had problems with the TypemockStart, specifically the unpacking of the properties in the InternalExecute method. The reflected code by JustDecompile was full of what looked to be duplicated array copying which did not compile. So I simplified this to map the properties to the right names.

You can download a copy of my Typemock.BM.TFS2012.Dll from here, so you don’t have to go through the process yourself.

I now had a TFS 2012 custom build activity. I took this new activity and put it the CustomActivities folder. Next I took an unedited version of the default 2012 build process template and added these new Typemockregister, TypemockStart (at the start of the test block) and TypemockStop (at the end of the test block) activities as well as a Typemock argument (of TypemockSettings type). I checked this new template into TFS, and then created a build setting the Typemock argument settings.

 

image

Now at this point it is worth mentioning the nice feature of AutoDeploy. This allows you to use Typemock without having it installed on the build agent, thus making build agent management easier. You copy the AutoDeploy folder from the Typemock installation folder into source control (though a rename might be sensible so you remember it is for Typemock auto deployment and not anything else). You can then set the four argument properties

  • The location of the auto deployment folder in source control
  • A switch to enable auto deployment
  • Your Typemock license settings.

By using the auto deployment feature I was able to uninstall Typemock on the build agent.

So I tried a build using these setting, all the build activities loaded Ok and the TypemockSettings was read, but my project compile failed. As I had uninstalled Typemock on my build agent all the references to Typemock assemblies in the GAC failed. These references were fine on a development PC which had Typemock installed not on the build agent which did not.

So I needed to point the references in my project to another location. Typemock have thought of this too and provide a tools to remap the references that you can find on the Typemock menu

image

You can use this tool, or do it manually.

You could re-point the references to the same location you used for the AutoDeploy feature. However I prefer to keep my project references separate to my infrastructure (build activities etc.) as I use the same build templates cross project. For our projects we arrange source control so we have the structure in the general form (ignoring branch for simplicity)

/$
A team project
      BuildProcessTemplate
             CustomActivities
             AutoDeploy
      MyProject-1
             Src 
                   Solution1A.sln
             Lib 
                   [Nuget packages]
                   AutoDeploy
                   other assemblies
      MyProject-2
             Src 
                   Solution2a.sln
             Lib 
                   [Nuget packages]
                   AutoDeploy
                   other assemblies

I make sure we put all assemblies referenced in the lib folder, including those from Nuget using a nuget.config file in the src folder with the SLN file e.g.

<settings>
  <repositoryPath>..\Lib\</repositoryPath>
</settings>

This structure might not be to your taste, but I like it as it means all projects are independent, and so is the build process. The downside is you have to manage the references for the projects and build separately, but I see this as good practice. You probably don’t share want to reference and Nuget packages between separate projects/solutions.

So now we have a 2012 build process that can start Typemock Isolator, and a sample project that contains Typemock based tests, some using MSTest and some using XUnit (remember Visual Studio 2012 support multiple unit testing frameworks not just MSTest, see here on how to set this up for TFS build). When the build is run I can see all my unit tests pass to Typemock isolator must be starting correctly

 

image

So for me this is a reasonable work around until Typemock ship a TFS 2012 specific version. Hope this file saves you some time if you use Typemock and TFS 2012.

Why Typemock Isolator does not work on TFS 2012 Build and what you can do about it

If you are using Typemock Isolator in your unit testing then you be wanting to include then in your automated build process. Unlike other mocking frameworks you have to do a bit of work to achieve this with Isolator, this is because to enable its advanced features of mocking items like sealed private classes you have to start the interceptor that does the magic prior to running the tests and the switch it off afterwards.

In the past I posted on how you could use a build activity I wrote to wrapper MStest in a TFS 2010 build using TMockRunner. Since I wrote this Typemock released a set of build activities to do the starting and stopping of the inceptor as separate activities, a much more flexible solution which I would always recommend for TFS.

However when you try to use either with TFS 2012 you get a problem, the activities fail to load. This is a problem we saw on the TFS Extensions Codeplex project; you have to build you activities against either the 2010 TFS API or the 2012 TFS API. You don’t need to alter the code in your activities, but you do need to make a specific build.

So at this time there is no solution, one or both of these activities need to be rebuilt. For the MSTest wrapper I wrote the source is available so you can do it yourself if you want to, but the way Typemock have implemented their activities is a better solution. This is because it is not reliant on TMockRunner and MStest, it can wrapper any activities. This is important as to be able to use the new ‘any framework’ unit testing features in VS2012 you want to use Microsoft’s new test running activities and not just the old MSTest activity.

I understand that Typemock are looking at releasing a TFS2010 version of their activities soon, but I know of no release date as yet. If you want an immediate solution you will need to do a bit of work.

  • You could rebuild my MSTest based activity
  • You could use the standard InvokeMethod activity and put the contents of my MStest activity’s generated command line into this
  • But the one I favour is to use a dissembler such as Telerik JustDecompile to get the code from the Typemock.TFS2010.DLL and build a new activity.

However, it must be said I see this as just a temporary measure until the official Typemock 2012 activity is released. I am not sure I will get around to doing this before the Typemock release, we shall see.