PowerShell script to convert VSTS tests to NUnit

January 9, 2010

one comment

A while ago we decided to convert all our test projects to NUnit. We found Microsoft’s test framework to be overly complicated, much slower, and with less features.

So, in order to make the transition easier, I wrote this PowerShell script to convert VSTS test projects and files to NUnit. I didn’t know any PowerShell beforehand, so it took me a bit to get it right, but the script works nicely now. We’ve run it against dozens of test projects (VS 2005, VS 2008, and it also works with VS 2010 beta 2, as I tried it at home) in our company and it did the job. It should be noted that the script only works against NUnit 2.5 and above, and while it has worked well for us, you’re using it at your own risk. If you have source control, and of course you do, then you’re not risking anything anyway 🙂

You can download the script right here.

In order to use it you’ll need to:

  1. Download and install PowerShell. You already have it if you’re running Windows 7.
  2. Run PowerShell, and run Set-ExecutionPolicy “unrestricted”, so you’ll be able to run scripts. Under Vista\7 you’ll have to run PowerShell as administrator to get this done.
  3. Copy the script to a solution or a VSTS test project folder (I do recommend doing the conversion one project at the time, but the script just scans for .cs and .csproj files, so you can run it against any number of projects). This step is optional, you can also provide the working directory as a parameter to the script.
  4. Check-out all the project’s files from source control, if you need to.
  5. In PowerShell, run “.\VstsToNUnit.ps1 [NUnit Version] [Path from .csproj file to NUnit.framework.dll] [Working directory (optional if the script is copied to the project folder)]
    Example: .\VstsToNunit.ps1 ..\lib\nunit.framework.dll c:\myprojects\MyVisualStudioProject

What does this script do? Well, its essentially a bunch of regex replacements running against every .cs and .csproj file in the working directory. It replaces the VSTS attributes with NUnit ones, changes VSTS test projects to standard class libraries, and adds a reference to NUnit instead of the VSTS one. It will also:

  1. Save you from a few gotchas, such as the arguments order in Assert.IsInstanceOfType being different between the two frameworks.
  2. Remove some TestContext stuff Visual Studio generates automatically when you use the unit-tests generation feature. Note, however, that if you actually used the TestContext object you’ll have to refactor your unit tests.
  3. For users of Rhino.Mocks.Constraints, adds stuff like “using Is=Rhino.Mocks.Constraint.Is”, as NUnit also defines Is\List\Text classes, and it causes ambiguity when wanting to use the Rhino Mocks constraints.

Stuff the script won’t do:

  1. As I said, remove all the TestContext references. You’ll need to get some of those manually.
  2. Remove the [DeploymentItem] attribute. You’ll need to manually delete those, while making sure your test files are copied to the output directory for NUnit to find (right-click the file in your solution, and in the properties select “Copy to output directory – always”.
  3. Delete .vsmdi and .testrunconfig files. You’ll have to get them yourself. Wouldn’t want to steal that joy from you 🙂
  4. Perhaps a bunch of other VSTS specific stuff our projects don’t use.

If you’re using this, please let me know if you ran into any issue, or anything that should be added to the script, and I will update it here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


one comment

  1. LinJuly 21, 2012 ב 13:55

    Spot on with this write-up, I absolutely believe this site needs much more attention.
    I’ll probably be returning to read through more, thanks for the advice!