Migrating from Nunit 1.0 to Nunit 2.2.2

By Eli Ofek at April 03, 2005 21:39
Filed Under: Proffesional, Proffesional
Hi all,
One of our projects at work began 3 years ago.
at that time we searched for a Unit Testing tool for .Net.
After trying some options, we made the right decision and chose Nunit 1.0 as the Unit Testing tool for the project.
Nunit GUI 1.0 was not perfect for our requirements at that time, so we used the benefits of open source code and
inserted some light changes, that later on became a natural part of Nunit on the Public version.
When Nunit grow and developed it became an excellent tool,
only we could not start to use it easily as Nunit 1.0 was not Attribute based, and we had tons of "Legacy"
Unit testing code in our hands and a tight project deadline ...
So it became that after 3 years of coding, the project stayed with Nunit 1.0.
Only recently the team has decided that enough is enough and it's time to move on.
After consulting with Roy Osherove we have decided on a general plan of converting the code
by using an evaluation version of Resharper & Modifying the Nunit 1.0 open source interfaces
to become as close as possible to Version 2.2.2.
Me and Galia decided to take responsibility for the mission and transformed the code in less then a day of work.
We decided to share the experience in case there are other people around stuck in the same problem ...
Here is the recipe for transforming Nunit 1.0 Tests into Nunit 2.2.2 Tests
A bit of technical information about the project:
  • Over 50 Assemblies
  • Written in C#.
  • Using Visual Studio 2003.
  • Visual Source Safe 6.0d Source Control
Here are the steps we went through:
   "Transformation milestone X" is defined as the actions:
  1. Check in all you Code ( Keeping it checked out, ore checking it out again)  commenting it with the changes made so far.
  2. Create a label "Nunit Upgrade - X"
  • Coordinate a day with all the development team so that you can check out all the code without intervention
    • (Unless you are working with a Source Control System that allows nice branching)
  • Install Resharper in the Workstation.
    • If you have 99$ to spare, it will be a money well spent in my opinion.
    • If not, use the evaluation version as we did, and uninstall it when you are done.
  • Make sure all code is checked in the source control .
  • Clear your working folder on the hard drive.
  • Get all code from source control and Re build all projects.
  • Run all Unit tests in all the projects to make sure all is green and that we are starting on a "clean surface".
  • In case you have multiple projects spreading over a few solutions, create one solution with all the projects.
  • Create a Label in the Source Control named "Before Nunit upgrade"
  • Check out all code in the new solution.
  • Rename all methods to begin with "Assertion."
    • This conversion was done using the IDE's  Find & Replace dialog.
    • Replace all "TestCase." instances with "Assertion."Find instances with one white space before name and ( after. and replace them.   
       According to your coding conventions, you might need to change the spacing, tabs prefixes and so on..          
    • Here are the strings we needed to replace ( for some projects you might need more)
      • "AssertEquals("
      • "AreNotNull("
      • "AssertNull("
      • "AssertSame("
      • "Assert("       - This one is done last !
    • If code uses C#'s Debug.Assert, correct it using the transformation: Debug.Assertion -> Debug
  • Create Transformation milestone "After renaming methods to static formation"
  • Add Nunit 1.0 source code to the solution.
    • You can download the source from here.
    • You only need to add the NunitCore project.
    • Rename the project from NunitCore to NunitCoreOld
  • Change all assembly references from the binary file to the added projects.
    • We know .. it's dirty work, but we didn't think it was time worthy to automate this process...
    • Save & Build all. Check that there are no compilation errors.
      • If done correctly, none should be.
      • If there are, fix them.
  • Using Resharper , Auto rename all public methods of "Assertion" class in the NunitCoreOld project.
    • These are the transformation we used. We actually found out not all the methods were is use, so we got lazy and did only the ones we needed. you might need to add some more to the list. simply check out the new API documentation for differences.
      • Assert -> IsTrue
      • AssertEquals -> AreEqual
      • AssertNotNull -> IsNotNull
      • AssertNull -> IsNull
      • AssertSame -> AreSame
    • Rename "ExpectException" attribute class to "ExpectedException"
  • Save & Compile code to make sure there are no errors, If there are any, fix them.
  • Now we need to change all parameters order according to the new API in the "Assertion" class using Resharper.
    • The Rule is "For all public methods that has a string as a first parameter, move it to be the last.
    • For methods that has 3 string parameters, sample the transformed code to see it was done correctly. We found a problem, didn't bother to make sure if it was a software error or a human error.
  • Save & Compile code to make sure there are no errors, If there are any, fix them.
  • Using Resharper, rename the "Assertion" class to "Assert".
  • Save & Compile code to make sure there are no errors, If there are any, fix them.
  • Create Transformation milestone "After Resharper transformation" .
  • Install Nunit 2.2.2
    • You can download the installation package here.
  • Change all project references from NunitCoreOld  to the new nunit.framework binary in the GAC from Version 2.2.2.
  • Remove NunitCoreOld from the Solution.
  • Create Transformation milestone "Referencing Version 2.2.2" .
  • Now we need to deal with adding the new Attributes of the 2.2.2 framework to the tests.
    • We did all the transformation in this section using the IDE's Find & Replace dialog with the Regular Expressions checkbox enabled.
    • Here are all the transformation & Regular expressions used to make them:
      • TestCase inheritance -> [TestFixture]
        • public class {:c+}.*TestCase -> [TestFixture]\n\tpublic class \1
      • Setup method ->  [SetUp] :
        • protected override void SetUp -> [SetUp]\n\t\tpublic void SetUp 
      • TearDown method -> [TearDown]
        • protected override void TearDown -> [TearDown]\n\t\tpublic void TearDown
      • TestMethod - > [Test]
        • public void Test -> [Test]\n\t\tpublic void
        • ( notice one white space at the end of the replace string ! )
      • Removing Inheritance:
        • \([Ss]tring name\).*\n*.*base\(name\) -> \(\)
  • Save & Compile code to make sure there are no errors, If there are any, fix them.
  • Create Transformation milestone "Changing to Attribute formation" .
  • Run all tests using the new Nunit 2.2.2 GUI.
    • You might find some errors created by bad transformation . we found only 2. it took 5 min. to solve them.
  • Create Transformation milestone "Ready for Nunit 2.2.2" .
  • Delete old Nunit Source code from Source Control & File system. (Normal delete, not permanent)
  • Delete old Nunit 1.0 binaries from source control * File system. (Normal delete, not permanent)
  • Optionally, Install "Test Driven .Net" for easy usage in development process.
That's all ! Your'e done !
And it took us less then a day.
Should take you less because you have all the process figured out already right here :-)
Good Luck !
Eli & Galia.

Add comment

  Country flag
  • Comment
  • Preview



<<  May 2021  >>

View posts in large calendar

Page List


    I work for Microsoft Israel as s Senior Premier Field Engineer.
    The opinions expressed here are my own personal opinions and do not represent my employer's view in anyway.