Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Extension Methods and Unit Testing in VS2008

Rate me:
Please Sign up or sign in to vote.
4.17/5 (12 votes)
4 Nov 2007CPOL8 min read 50.2K   133   39  
Create some useful Extension Methods using .NET 3.5, then test your new methods using the built-in unit testing functionality in VS2008 Professional.
  • extensionunittests_src.zip
    • ClassLibrary1
      • bin
        • Debug
          • ClassLibrary1.exe
          • ClassLibrary1.vshost.exe
      • ClassLibrary1.csproj
      • ClassLibrary1.sln
      • ClassLibrary1.vsmdi
      • Extensions.cs
      • LocalTestRun.testrunconfig
      • Properties
      • test.cs
      • TestResults
        • Administrator_STORMDEV 2007-11-04 10_44_59.trx
        • Administrator_STORMDEV 2007-11-04 10_44_59
          • Out
            • ClassLibrary1.exe
            • ClassLibrary1.pdb
            • utilstests.dll
            • UtilsTests.pdb
        • Administrator_STORMDEV 2007-11-04 10_46_43.trx
        • Administrator_STORMDEV 2007-11-04 10_46_43
          • Out
            • ClassLibrary1.exe
            • ClassLibrary1.pdb
            • utilstests.dll
            • UtilsTests.pdb
        • Administrator_STORMDEV 2007-11-04 10_55_32.trx
        • Administrator_STORMDEV 2007-11-04 10_55_32
          • Out
            • ClassLibrary1.exe
            • ClassLibrary1.pdb
            • utilstests.dll
            • UtilsTests.pdb
        • Administrator_STORMDEV 2007-11-04 11_06_39.trx
        • Administrator_STORMDEV 2007-11-04 11_06_39
          • In
            • STORMDEV
          • Out
            • ClassLibrary1.exe
            • ClassLibrary1.pdb
            • utilstests.dll
            • UtilsTests.pdb
      • UtilsTests
using Utils.MyExtensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UtilsTests
{
    
    
    /// <summary>
    ///This is a test class for JunkExtensionsTest and is intended
    ///to contain all JunkExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class JunkExtensionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ToSomethingElse
        ///</summary>
        [TestMethod()]
        public void ToSomethingElseTest()
        {
            string input = "abc123";
            string expected = "    abc123";
            string actual = input.ToSomethingElse();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsNullOrEmpty
        ///</summary>
        [TestMethod()]
        public void IsNullOrEmptyTest()
        {
            string input = string.Empty;
            bool expected = false;
            bool actual = input.IsNullOrEmpty2();
            Assert.AreEqual(expected, actual);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions