locked
Convert unit test from NUnit RRS feed

  • Question

  • Hi

    Is there a tool that convert unit test code from NUnit to TFS 2005 unit test

    Ghassan
    Thursday, May 14, 2009 8:44 PM

Answers

  • Hi !

    AFAIK the NUnit converter is not updated anymore, and there doesn't seem to be any other tools out there.  However, the steps to perform the conversion are rather trivial, so it can be automated, as far as your projects doesn't use any of the more special NUnit tags, as for example the Explicit tag. 
    The post in this link, http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/433e4860-b61f-44fd-bef9-a569fb32d244, contains all the steps you need to go through in order to convert your projects.  Some of these can be automated.

    You can do a lot of this by just using Macro's in Visual Studio.  I'm no VB.net expert (C# here, but Macro's have to be written i vb), so a script for changing the C# files from NUnit to MSTest, can look like this:
    Sub ConvertNUnit2MSTest()
    
            Dim source As List(Of String)
            source = New List(Of String)()
            source.Add("[TestFixture]")
            source.Add("[Test]")
            source.Add("[SetUp]")
            source.Add("[TearDown]")
            source.Add("[TestFixtureSetUp]")
            source.Add("[TestFixtureTearDown]")
            source.Add("using NUnit.Framework;")
            Dim target As List(Of String)
            target = New List(Of String)()
            target.Add("[TestClass]")
            target.Add("[TestMethod]")
            target.Add("[TestInitialize]")
            target.Add("[TestCleanup]")
            target.Add("[ClassInitialize]")
            target.Add("[ClassCleanup]")
            target.Add("using Microsoft.VisualStudio.TestTools.UnitTesting;")
    
            Dim n As Integer
            n = 1
            For Each s In source
                n = source.IndexOf(s)
                DTE.ExecuteCommand("Edit.ReplaceinFiles")
                DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Activate() 'Find and Replace
                DTE.Find.MatchCase = True
                DTE.Find.MatchWholeWord = True
                DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
                DTE.Find.FindWhat = source(n)
                DTE.Find.ReplaceWith = target(n)
               DTE.Windows.Item(Constants.vsWindowKindMacroExplorer).Activate()
                DTE.Find.FindWhat = source(n)
                DTE.Find.ReplaceWith = target(n)
                DTE.Find.Target = vsFindTarget.vsFindTargetFiles
                DTE.Find.MatchInHiddenText = True
                DTE.Find.SearchPath = "Entire Solution"
                DTE.Find.SearchSubfolders = True
                DTE.Find.KeepModifiedDocumentsOpen = False
                DTE.Find.FilesOfType = "*.cs;"
                DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
                DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
                DTE.Find.Execute()
            Next
    
            DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
        End Sub
    Copy/Paste this into a Macro project, and run it. 
    You can add a similar change to the csproj file, to change the references, also.

    best regards
    Terje


     
    • Proposed as answer by Bill.Wang Tuesday, May 19, 2009 9:16 AM
    • Marked as answer by Bill.Wang Thursday, May 21, 2009 8:37 AM
    Monday, May 18, 2009 8:14 AM

All replies

  • See thread http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/872fe600-b8af-4195-9640-97d93c73c18b
    Ewald - Please remember to mark the replies as answers if they help.
    Thursday, May 14, 2009 8:56 PM
  • Hi Ghassan,

    There is a tool which was hosted on GotDotNet but unfortunately I could not find it on CodePlex. I have a copy with a complete tutorial which I have uploaded on my skydrive at http://cid-5d4404317146129c.skydrive.live.com/self.aspx/NUnit%20Migration/NUnitConversionSetup.zip

    It essentially converts the attributes TestFixture to TestClass and Test to TestMethod.  I am giving an example here which can be used as a guideline if you are going to do one off kind of migration. For a bulk / pro job you need the converter.

    Original Code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using NUnit.Framework;
    
    namespace NUnitConversion
    {
        [TestFixture]
        public class NumericalConversion
        {
            [Test]
            public void Int32Int64Comparison()
            {
                long l64 = 0;
                int i32 = 0;
                Assert.AreEqual(i32, l64);
            }
    
            [Test, ExpectedException(typeof(AssertionException))]
            public void NanEqualsNaNFails()
            {
                Assert.AreEqual(Double.NaN, Double.NaN, 0.0);
            }
    
        }
    }
    
    


    Whereas migrated code will look like:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace NUnitConversion
    {
        [TestClass()]
        public class NumericalConversio
    VP, Technology, SEED Infotech Ltd.
    Friday, May 15, 2009 11:13 AM
  • Hi Ghassan,

    There is a tool which was hosted on GotDotNet but unfortunately I could not find it on CodePlex. I have a copy with a complete tutorial which I have uploaded on my skydrive at http://cid-5d4404317146129c.skydrive.live.com/self.aspx/NUnit%20Migration/NUnitConversionSetup.zip

    It essentially converts the attributes TestFixture to TestClass and Test to TestMethod.  I am giving an example here which can be used as a guideline if you are going to do one off kind of migration. For a bulk / pro job you need the converter.

    Original Code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using NUnit.Framework;
    
    namespace NUnitConversion
    {
        [TestFixture]
        public class NumericalConversion
        {
            [Test]
            public void Int32Int64Comparison()
            {
                long l64 = 0;
                int i32 = 0;
                Assert.AreEqual(i32, l64);
            }
    
            [Test, ExpectedException(typeof(AssertionException))]
            public void NanEqualsNaNFails()
            {
                Assert.AreEqual(Double.NaN, Double.NaN, 0.0);
            }
    
        }
    }
    
    


    Whereas migrated code will look like:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace NUnitConversion
    {
        [TestClass()]
        public class NumericalConversion
        {
            [TestMethod()]
            public void Int32Int64Comparison()
            {
                long l64 = 0;
                int i32 = 0;
                Assert.AreEqual(i32, l64);
            }
    
            [TestMethod(), ExpectedException(typeof(AssertFailedException))]
            public void NanEqualsNaNFails()
            {
                Assert.AreEqual(Double.NaN, Double.NaN, 0.0);
            }
    
        }
    }
    
    

    VP, Technology, SEED Infotech Ltd.
    Friday, May 15, 2009 11:14 AM
  • Thanks a lot Subodh

    I appreciate your effort for help.

    Unfortunetly, this tool is for VS 2005

    All my projects are for 2008.

    If you, or anybody knows where I can find something useful for 2008, please let me know.

    Thanks any

    Friday, May 15, 2009 8:30 PM
  • Oh, I am sorry Ghassen but in your initial post you had mentioned TFS 2005 so I thought that this tool would be of use.
    VP, Technology, SEED Infotech Ltd.
    Saturday, May 16, 2009 4:11 AM
  • Hi !

    AFAIK the NUnit converter is not updated anymore, and there doesn't seem to be any other tools out there.  However, the steps to perform the conversion are rather trivial, so it can be automated, as far as your projects doesn't use any of the more special NUnit tags, as for example the Explicit tag. 
    The post in this link, http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/433e4860-b61f-44fd-bef9-a569fb32d244, contains all the steps you need to go through in order to convert your projects.  Some of these can be automated.

    You can do a lot of this by just using Macro's in Visual Studio.  I'm no VB.net expert (C# here, but Macro's have to be written i vb), so a script for changing the C# files from NUnit to MSTest, can look like this:
    Sub ConvertNUnit2MSTest()
    
            Dim source As List(Of String)
            source = New List(Of String)()
            source.Add("[TestFixture]")
            source.Add("[Test]")
            source.Add("[SetUp]")
            source.Add("[TearDown]")
            source.Add("[TestFixtureSetUp]")
            source.Add("[TestFixtureTearDown]")
            source.Add("using NUnit.Framework;")
            Dim target As List(Of String)
            target = New List(Of String)()
            target.Add("[TestClass]")
            target.Add("[TestMethod]")
            target.Add("[TestInitialize]")
            target.Add("[TestCleanup]")
            target.Add("[ClassInitialize]")
            target.Add("[ClassCleanup]")
            target.Add("using Microsoft.VisualStudio.TestTools.UnitTesting;")
    
            Dim n As Integer
            n = 1
            For Each s In source
                n = source.IndexOf(s)
                DTE.ExecuteCommand("Edit.ReplaceinFiles")
                DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Activate() 'Find and Replace
                DTE.Find.MatchCase = True
                DTE.Find.MatchWholeWord = True
                DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
                DTE.Find.FindWhat = source(n)
                DTE.Find.ReplaceWith = target(n)
               DTE.Windows.Item(Constants.vsWindowKindMacroExplorer).Activate()
                DTE.Find.FindWhat = source(n)
                DTE.Find.ReplaceWith = target(n)
                DTE.Find.Target = vsFindTarget.vsFindTargetFiles
                DTE.Find.MatchInHiddenText = True
                DTE.Find.SearchPath = "Entire Solution"
                DTE.Find.SearchSubfolders = True
                DTE.Find.KeepModifiedDocumentsOpen = False
                DTE.Find.FilesOfType = "*.cs;"
                DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
                DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
                DTE.Find.Execute()
            Next
    
            DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
        End Sub
    Copy/Paste this into a Macro project, and run it. 
    You can add a similar change to the csproj file, to change the references, also.

    best regards
    Terje


     
    • Proposed as answer by Bill.Wang Tuesday, May 19, 2009 9:16 AM
    • Marked as answer by Bill.Wang Thursday, May 21, 2009 8:37 AM
    Monday, May 18, 2009 8:14 AM