none
Sharing App.Config between test projects RRS feed

  • Question

  • I have one Commom.Test project in one solution and then i have several other test projects in their own solutions. These other test projects requires common app settings. So instead of adding config file in each of the test project I want to share one config file between all these other test projects. So i have added App.Config in Common.Test project and then added this config file "as a link" is all other projects.

    Is that the right way to do or is there any other approach to share appsettings between test projects. 

    Thursday, November 3, 2011 4:35 PM

All replies

  • Hi,

    I did something similar in one of my projects.  I wanted to use Dependancy Injection to allow my classes to know if they were being run from Unit Tests or not.

    I created a $(SolutionName).Common project and within that created  an "AppConfigValues.cs" class. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Globalization;
    
    namespace AspenToDeq.Common.DataContainers
    {
        /// <summary>
        /// A Class that allows values to be read from the App.Config file.<br />
        /// This class is designed to be used with Dependancy Injection.<br />
        /// <br />
        /// This class determines if the application is being run from Unit Tests 
        /// or from Production. 
        /// </summary>
        public class AppConfigValues
        {
            private bool _isTestMode;
    
            /// <summary>
            /// Default constructor sets class to production mode.
            /// </summary>
            public AppConfigValues()
            {
                _isTestMode = false;
            }
    
            /// <summary>
            /// Overloaded constructor with any string parameter value sets class to test mode.
            /// </summary>
            /// <param name="testMode">Any string value.</param>
            public AppConfigValues(string testMode)
            {
                _isTestMode = true;
            }
    
            /// <summary>
            /// Static class factory method.
            /// </summary>
            /// <returns>AppConfigValues instance.</returns>
            public static AppConfigValues Create()
            {
                return new AppConfigValues();
            }
    
            /// <summary>
            /// Static class factory list method.
            /// </summary>
            /// <param name="dummyValue">Any string value will set the class to test mode.</param>
            /// <returns>List of AppConfigValues records.</returns>
            public static AppConfigValues Create(string dummyValue)
            {
                return new AppConfigValues(dummyValue);
            }
    
            /// <summary>
            /// Gets a value indicating whether this instance is in test mode.
            /// </summary>
            /// <value>
            /// <c>true</c> if this instance is test mode; otherwise, <c>false</c>.
            /// </value>
            public bool IsTestMode
            {
                get { return _isTestMode; }
            }
        }
    }
    

    From my unit test suite I include the class with the following code.

            private AppConfigValues diAppConfigValues;
    
            /// <summary>
            /// Initializes an AspenChemResultRecord with the 
            /// </summary>
            [TestInitialize()]
            public void MyTestInitialize()
            {
                target = DataContainers.AspenChemResultRecord.CreateAspenChemResultList();
                target.Add(CreateGenericResultRecord());
    
                // Use Constructor Dependancy Injection
                diAppConfigValues = AppConfigValues.Create("Test Mode");
            }
    

    This class allows me to be able to tell if I'm being run from a Unit Test for Every Workflow in my Solution.

    This is similar to what you proposed because the functionality in a Common project and the same configuration information is available to multiple projects without having to duplicated code.

    Neil Squires

    Wednesday, January 23, 2013 7:07 PM