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:
- Download and install PowerShell. You already have it if you’re running Windows 7.
- 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.
- 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.
- Check-out all the project’s files from source control, if you need to.
- 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 126.96.36.199 ..\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:
- Save you from a few gotchas, such as the arguments order in Assert.IsInstanceOfType being different between the two frameworks.
- 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.
- 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:
- As I said, remove all the TestContext references. You’ll need to get some of those manually.
- 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”.
- Delete .vsmdi and .testrunconfig files. You’ll have to get them yourself. Wouldn’t want to steal that joy from you 🙂
- 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.