General Ramblings comments edit

From a conversation at today’s staff meeting…

Dev 1: I’ve been catching up on all the old James Bond movies recently. We just watched Dr. No last night.

Me: Oh, that’s a good one. Hey, which is your favorite Bond? Actor, not movie.

Dev 1: Well, let’s see, there’s Sean Connery, George Lazenby, Roger Moore, Timothy Dalton, Pierce Brosnan, Daniel Cra-

Dev 2: Chuck Norris.

Me: What?

Dev 2: Chuck Norris. I saw this web site that says he can do anything.

So, folks, there you have it: Chuck Norris is the best James Bond.

dotnet, vs comments edit

I’ve blogged before about getting Typemock, NUnit, and NCover all working together in MSBuild. Though that’s admittedly a tad stale, with a bit of tweaking the contents of that article still apply.

I got a question about running tests that use Typemock Isolator outside of Visual Studio, though, so I figured I’d post this article with some additional info and clarifications.

First, the setup:

  • NCover 3.4.16
  • Typemock Isolator 6.0.6
  • MSTest with Visual Studio 2010

If you have different versions of these tools, you may need to tweak things. Also, I’m building on a 64-bit machine, so you may see some paths referring to 32-bit over 64-bit tools because MSTest is a 32-bit runner so you need to use the 32-bit NCover to get coverage.

When you have Typemock Isolator installed, running tests through the built-in Visual Studio test runner “just works” because Isolator installs a Visual Studio add-in helper. To get coverage, you can use TestDriven.NET to “Test With -> NCover” and it works great.

If you want to run coverage outside of Visual Studio, though, there are a few things you might think to try, some of which work and some of which don’t.

THE BIG TAKEAWAY: You have to start things in a specific order.

  1. Start Typemock so it can link with NCover.
  2. Start NCover so it can run and profile your unit tests.
  3. Start your unit test runner so NCover can gather statistics.
  4. When the test runner ends, NCover automatically ends.
  5. Make sure Typemock stops when everything is over, regardless of whether the tests pass or fail.

If you don’t start things in the right order, your tests won’t work and you won’t get the expected results.

WHY IS THAT ORDER REQUIRED?

The way Isolator works, it’s sort of a “pass-through profiler.” NCover is a profiler, too, which is how it takes coverage statistics. You can only have one profiler running at one time. The cool “trick” Isolator does is that it “links” with other profilers so calls pass through Isolator first, your mocks get inserted, and then pass along to the linked profiler like NCover. You can actually watch Typemock switch registry entries around on the fly when you start and stop it - it’ll temporarily put itself into the registry where you’d expect to see NCover, so if you “start NCover” you’re actually starting Typemock, which then chains in NCover.

However, if you try to start the other profiler like NCover first, the linking doesn’t happen so your mocks don’t show up when you expect them. Problems.

Given that, let’s talk about ways to run Typemock Isolator and get coverage when outside of Visual Studio.

Build Scripts

Running things through a build script is the most common and recommended way of doing things. It allows you to automate the whole build process and use the same script on a developer machine and in a continuous integration server.

Let me drop some code on you and then we’ll walk through it:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="All" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <!-- Coverage logs and such will be placed here. -->
    <LogDirectory>$(MSBuildProjectDirectory)\log</LogDirectory>

    <!-- Build configuration (Debug or Release). -->
    <BuildConfiguration>Debug</BuildConfiguration>

    <!-- Path to the NCover 32-bit installation (MSTest is 32-bit). -->
    <NCoverPath>$(ProgramFiles)\NCover\</NCoverPath>

    <!-- Path to the NCover build tasks (different path than NCover 32-bit on a 64-bit machine). -->
    <NCoverBuildTasksPath>$(ProgramW6432)\NCover\</NCoverBuildTasksPath>

    <!-- Path to the Typemock Isolator installation. -->
    <TypemockPath>$(ProgramFiles)\Typemock\Isolator\6.0\</TypemockPath>

    <!-- Path to the unit test assembly for easier test execution. -->
    <UnitTestAssembly>$(MSBuildProjectDirectory)\CoverageDemoTests\bin\$(BuildConfiguration)\CoverageDemoTests.dll</UnitTestAssembly>
  </PropertyGroup>

  <!-- Get the Typemock and NCover build tasks. -->
  <Import Project="$(TypemockPath)\TypeMock.MSBuild.Tasks"/>
  <UsingTask TaskName="NCover.MSBuildTasks.NCover" AssemblyFile="$(NCoverBuildTasksPath)Build Task Plugins\NCover.MSBuildTasks.dll"/>

  <Target Name="All" DependsOnTargets="Clean;Compile;Test" />
  <Target Name="Clean">
    <Message Text="Removing build output artifacts in preparation for a clean build." />
    <RemoveDir Directories="$(LogDirectory)" ContinueOnError="true" />
    <RemoveDir Directories="$(MSBuildProjectDirectory)\CoverageDemo\bin" ContinueOnError="true" />
    <RemoveDir Directories="$(MSBuildProjectDirectory)\CoverageDemo\obj" ContinueOnError="true" />
    <RemoveDir Directories="$(MSBuildProjectDirectory)\CoverageDemoTests\bin" ContinueOnError="true" />
    <RemoveDir Directories="$(MSBuildProjectDirectory)\CoverageDemoTests\obj" ContinueOnError="true" />
  </Target>
  <Target Name="Compile">
    <Message Text="Compiling the solution." />
    <MSBuild Projects="CoverageDemo.sln" Targets="Build" Properties="Configuration=$(BuildConfiguration)" />
  </Target>
  <Target Name="Test">
    <MakeDir Condition="!Exists('$(BuildLogDirectory)')" Directories="$(BuildLogDirectory)"/>
    <CallTarget Targets="Test_BuildTasks;Test_CommandLine" />
  </Target>
  <Target Name="Test_BuildTasks">
    <Message Text="Testing with Typemock and NCover build tasks." />
    <TypeMockStart Link="NCover3.0"/>
    <NCover
      ContinueOnError="false"
      ToolPath="$(NCoverPath)"
      TestRunnerExe="MSTest.exe"
      TestRunnerArgs="/testcontainer:&quot;$(UnitTestAssembly)&quot;"
      IncludeAssemblies="CoverageDemo"
      LogFile="$(LogDirectory)\Test_BuildTasks.log"
      CoverageFile="$(LogDirectory)\Test_BuildTasks.xml"
      IncludeAutoGenCode="false"
      RegisterProfiler="false"/>
    <CallTarget Targets="__TestFinally"/>
    <OnError ExecuteTargets="__TestFinally"/>
  </Target>
  <Target Name="Test_CommandLine">
    <Message Text="Testing with Typemock and NCover command lines." />
    <PropertyGroup>
      <!-- Path to Typemock Console Runner. -->
      <TMockRunner>$(TypemockPath)TMockRunner.exe</TMockRunner>

      <!-- Path to NCover.Console Runner. -->
      <NCoverConsole>$(NCoverPath)NCover.Console.exe</NCoverConsole>
    </PropertyGroup>
    <Exec Command="&quot;$(TMockRunner)&quot; -first -link NCover3.0 &quot;$(NCoverConsole)&quot; //x &quot;$(LogDirectory)\Test_CommandLine.xml&quot; //l &quot;$(LogDirectory)\Test_CommandLine.log&quot; //a CoverageDemo MSTest.exe /testcontainer:&quot;$(UnitTestAssembly)&quot;" />
  </Target>
  <Target Name="__TestFinally">
    <!-- Make sure we stop Typemock whether there's an error or success in the tests. -->
    <TypeMockStop/>
  </Target>
</Project>

Now, let’s walk through it.

The first thing we do is set up some helpful properties. This will make creation of the various command lines and such a little easier. In this case, it’s mostly paths to tools.

Next we include the Typemock and NCover build tasks. That way we can use those to run our tests.

The “All,” “Clean,” and “Compile” targets are standard fare.

  • The “All” target is our build script entry point. Run that target and it does the full clean/build/test run.
  • The “Clean” target deletes all the binaries and log files so we can get a nice clean build run.
  • The “Compile” target actually builds the assemblies. In this case, I have two - a class library and the corresponding set of unit tests.

The “Test” target creates the folder where we’ll dump our coverage logs and then fires off the unit testing.

Now we get to the interesting bit: Showing the two ways you can run tests with coverage.

The Test_BuildTasks target shows coverage using the provided build tasks. This is the recommended way of doing things since the build task interface makes your script a lot more readable and you get some “compile time checking” in case you mistype one of the build script attributes. Plus, in some cases the build script tasks make things a little easier to specify than the longer, more cryptic command lines. You’ll notice that we’re starting things and stopping them in the order mentioned earlier. That’s important, and it’s why this works.

The Test_CommandLine target shows coverage using a command line executable. Typemock Isolator comes with a program called “TMockRunner.exe” which is a lot like NCover.Console.exe that comes with NCover - it lets you start up a process that will have Typemock enabled on it. If you dissect that big long command line, you’ll see:

  • We lead with TMockRunner.exe, tell it we’ll be running coverage (-first) and link it to NCover (-link NCover3.0).
  • We run NCover.Console.exe with its usual command line options, telling it where to put logs and which assembly to profile.
  • Finally we run MSTest.exe and tell it where our unit tests are.

In the command line version, we don’t have to explicitly shut down Typemock Isolator because it’s only enabled for that one process, just as NCover.Console.exe only enables NCover for the one process it starts.

Command Line Execution

I showed you a command line in the build script example above, but you don’t have to use it inside a build script. It’ll work just as well outside the script environment. The only downside to using it alone is that you won’t be able to use the handy variables to make the command line more readable the way you can in the build script, but if you make a little batch file or something with the command line in it, that’ll work perfectly.

NCover Explorer

NCover Explorer offers a way to start an application and profile it from right in the UI.

NCover Explorer "New Project" settings
dialog

This won’t work the way you think because NCover Explorer tries to start NCover first. Remember the critical ordering, above, where Typemock Isolator needs to be started first? That doesn’t happen here. NCover Explorer expects to start NCover straight away. So how do you get it to work?

Start NCover Explorer using TMockRunner.exe and Typemock will be enabled during your test runs. A sample command line is as follows:

"C:\Program Files (x86)\Typemock\Isolator\6.0\TMockRunner.exe" -first -link NCover3.0 "C:\Program Files\NCover\NCover.Explorer.exe"

When you run that, the console window where you started NCover Explorer will stay open. Leave it. Now when you set up your project, set the application to profile as MSTest.exe and set your “testcontainer” to your unit test assembly:

"Application to Profile" settings in NCover
Explorer

And for NCover Path, make sure you point to the 32-bit version of NCover.Console.exe because MSTest.exe is 32-bit:

"NCover Path" settings in NCover
Explorer

Now when you click the “Run Coverage” button, things will work as expected because TMockRunner.exe has enabled Typemock Isolator inside NCover Explorer.

NUnit GUI Or Other Test Runner Tools

I know we’re using MSTest in this example, but I figured a quick note was in order:

If you’re using, say, NUnit and want Typemock to work inside the NUnit GUI, you need to do a similar trick as we did in NCover Explorer, above

  • start NUnit GUI through TMockRunner.exe, just omit the “-first” and “-link NCover3.0” command line options.  This same trick holds for other test runner tools - starting the tool through TMockRunner.exe should get you the results you’re looking for.

Hopefully this helps you get your tests running with Typemock Isolator outside Visual Studio. Happy testing!

dotnet, aspnet comments edit

I came across this trick while perusing the Autofac code base with the new MVC3 integration. You no longer have to register the HttpModule that disposes of request lifetime scopes because they do it for you dynamically. Figuring out how they did it revealed two really cool little tricks I’ve not seen documentation on.

Trick #1:System.Web.PreApplicationStartMethodAttribute

.NET 4 adds a new attribute that allows you to programmatically do things just before application startup (that is, before Application_Start in your Global.asax). ASP.NET MVC3, for example, uses this hook to register build providers for the Razor view engine so you won’t have to do it manually in web.config.

To use it, first create a static class with a static method in it that contains your application startup logic. Be sure to guard against it getting called twice by having a flag indicating if it was called (sort of the way you track whether Dispose was called):

namespace MyNamespace
{
  private bool _startWasCalled = false;
  public static class PreApplicationStartCode
  {
    public static void Start()
    {
      if(!_startWasCalled)
      {
        _startWasCalled = true;
        // Do your startup logic here.
      }
    }
  }
}

You don’t have to call your class “PreApplicationStartCode,” nor do you have to call the method “Start,” but that seems to be the convention.

Once you have that class and method, mark your assembly with the attribute and point to your method:

[assembly: PreApplicationStartMethod(typeof(MyNamespace.PreApplicationStartCode), "Start")]

When the application starts, the System.Web.Hosting.HostingEnvironment.Intialize() method calls System.Web.Compilation.BuildManager.CallPreStartInitMethods() (all of that is internal, of course) and magic happens - your application startup logic runs.

Trick #2:Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule

The Microsoft.Web.Infrastructure assembly seems to have appeared along with MVC3 and WebMatrix. The DynamicModuleUtility.RegisterModule method is a ridiculously helpful and equally ridiculously undocumented method that allows you to add an IHttpModule to the request pipeline programmatically so you don’t have to put an entry into web.config. You just pass it the type of the IHttpModule implementation and it gets added to the pipeline:

DynamicModuleUtility.RegisterModule(typeof(MyNamespace.MyHttpModule));

The only catch is… you need to call it just before application startup. (See where I’m going with this?)

Tie it together: Call DynamicModuleUtility.RegisterModule() from inside PreApplicationStartCode.Start() and you can programmatically add HttpModules to the request pipeline.

Pretty nifty, huh?

Again, I saw this first in the Autofac codebase, so props to Alex Meyer-Gleaves (who added that code to the MVC3 support in Autofac) for figuring that one out.

dotnet, build comments edit

I’m upgrading a project to the released version of WiX and found an issue that causes .wixobj files to be created in your source tree in unfortunate locations at build time. I’ve filed the issue on SourceForge, but for folks running across it, I thought I’d post here as well including the workaround.

If you have a .wixproj that contains .wxs files that are included via relative path outside of the folder structure below the .wixproj, the intermediate objects (.wixobj) get placed in odd/incorrect locations based on the source external .wxs files. What this looks like in the wild is that random .wixobj files just sort of “materialize” during the build and you can’t figure out where they’re coming from.

For example, say you have a folder structure like this:

trunk/
  product/
    solution/
      installer/
        ProductSetup.wixproj
        Product.wxs
  setup/
    CustomDialogs.wxs

The ProductSetup.wixproj includes the set of custom dialogs like this:

<Project>
  <ItemGroup>
    <Compile Include="..\..\..\setup\CustomDialogs.wxs">
      <Link>CustomDialogs.wxs</Link>
    </Compile>
  </ItemGroup>
</Project>

(.wixproj simplified for the example)

Given that the OutputPath for the project is relative - bin\$(Configuration) - and the IntermediateOutputPath is also relative - obj\$(Configuration) - I would expect that all .wixobj files get created in obj\$(Configuration)but they don’t.

Alternatively, I could accept (though it’d be unexpected) that intermediate output gets placed in obj\$(Configuration) relative to each .wxs file, so I might see trunk\setup\obj\Debug\CustomDialogs.wixobj in this example. This is also not what happens.

Instead, paths are calculated based on the relative location of the .wxs source combined with the project’s intermediate output path. That means, for this example:

trunk\product\solution\installer\obj\Debug (the intermediate output location of the .wixproj project)

combines with

..\..\..\setup (the location of the external .wxs file)

and you find the file

trunk\product\solution\setup\CustomDialogs.wixobj

gets created during the build process.

The workaround for this appears to be to manually specify ObjectPath on any included .wxs files, like:

<Compile Include="..\..\..\setup\CustomDialogs.wxs">
  <Link>CustomDialogs.wxs</Link>
  <ObjectPath>obj\$(Configuration)</ObjectPath>
</Compile>

This forces the .wixobj files to be created in the appropriate location.

UPDATE 3/21/2011: I got a report that just putting obj\$(Configuration) didn’t work for one user and they needed to add a trailing backslash, like obj\$(Configuration)\ to the path. I didn’t need that, but if the above isn’t working for you, try adding the backslash.

This behavior is new in WiX 3.5.2519.0 (the released/official 3.5 version) and did not exist in 3.5.2403.0.

process comments edit

There have been a lot of developer-related “sharpening the saw” articles published and almost all of them speak to the technical aspect of becoming a better developer. A couple of popular ones (Hanselman, Atwood) have some suggestions like having technical brown-bags or reading programming blogs. These are wonderful suggestions for improving your technical abilities as a developer.

However, if you look at the actual description of “sharpening the saw,” originally from Steven Covey’s 7 Habits of Highly Effective People, it talks about having a “balanced program for self-renewal.” If all you do as a developer is focus on increasing your technical skills, you’re not really keeping in balance.

Perhaps it’s time to broaden your thoughts on what it means, as a developer, to “sharpen the saw.” What can you do to increase your skills/value that doesn’t involve technical abilities? Here are some ideas:

  • Take a writing course. Your local higher-education facilities (and many correspondence schools) most likely offer courses in basic writing. Not writing code, just writing. Prose. Why is that important? Most likely you aren’t writing just code all day. Whether it’s email, design documentation, blog posts, or other communications, you’re writing. If you want to be sure to be understood and if you want your communications to come across in a reasonably professional and proficient fashion, you need to be able to write in a cohesive fashion with a minimal amount of grammatical problems. This is not to say you need to become a novelist or write for The New Yorker, but especially in this social networking day-and-age where spelling and grammar are pitched out the window in favor of shortcuts and 140-character limits, having a good set of solid, basic writing skills will help you long-term.
  • Learn about UI design and user experience. It’s pretty well known that developers generally create some pretty horrible UI out of the box. It’s not because developers are incompetent, but because when they’re crafting that UI they’re not thinking about design principles or the user experience of the thing. Like UI is best left to “those folks on the other side of the office with those fancy turtlenecks and their copies of Photoshop.” Just as developers need to understand testing and not defer to QA, it’s valuable for developers to understand at least fundamental ideas of UI design and user experience. At the very least, this will allow you to take part in conversations about the UI in a more meaningful fashion and understand not just what you’re doing when the UX team asks you to fix something, but also why you’re doing it.
  • Take a class on being a mentor. Hanselman mentions in his post that one idea to help sharpen technical skills is to create a mentorship program. That’s a really good idea. But do you know how to be a mentor? Do you need a mentor on how to be a mentor? What does “mentoring” even mean? If you have a mentor to help you learn new things, do you know how to be mentored? It sounds like it’s a simple thing, but think about it: Has someone ever asked you a question, and when you answered it you knew they didn’t understand the answer but you also didn’t know how to give them an answer they would understand? Learning how to answer questions (as a mentor) and learning how to ask the right questions (as a mentee) is huge. I’ve taken the Practical Leader class on peer mentoring and I honestly can’t recommend it enough. It’s one of the best courses I’ve ever taken and it totally changed the way I approach helping and teaching people.
  • Improve your interpersonal communication skills. If you’ve worked in any sort of team, you’ll have run into a situation where there was a confrontation between two team members that probably could have been handled better. In coding, for example, sometimes ego gets in the way of what’s best for the code. Maybe someone on the team has a strong personality and you don’t know how to make suggestions to them. Maybe someone on the team has a milder personality and you don’t know how to coax their input/feedback from them when trying to make decisions. Whatever the case may be, strengthening your communications skills will help you work better in the team. One course I’ve taken and highly recommend is the Vital Smarts “Crucial Conversations” training. It’s one of those classes where, as you’re going through it, it feels like it’s revealing information to you that you already knew but didn’t consciously recognize. Suddenly, being conscious of it, you realize all the things you did wrong in previous poor interactions, how things could have been handled better… and how you’ll approach things next time you encounter the situation. Well worth the time.

Again, the idea here is that there are things other than technical skills that will help you sharpen your saw as a developer.

What do you do in a non-technical capacity to sharpen your saw?