locked
How can I change the app.config file when I run tests with MSTest RRS feed

  • Question

  • Hello

    I have a unit test project with an App.config file. The file connects the endpoint to a service. I compile the project and I run the tests using 

    mstest.exe /testcontainer:D:\MyTest.dll and it works ok.

    Now make some copies to the App.config files and I change the service address (App_S01.config, App_S02.config) they all are located next to the test dll.

    How can I change the mstest command to run the test using one of these new app config files. I have tried

    mstest.exe /runconfig: D:\App_S01.config /testcontainer:D:\MyTest.dll

    but I receive the following error: The file 'D:\App_S01.config' has unknown format and cannot be converted to the current version.

    Thursday, May 12, 2011 9:55 AM

Answers

  • Hi Angela_HAC,

    I have reappeared this issue like this link http://nileshgule.blogspot.com/2010/09/unit-test-application-configuration.html

    1.      I create a simple App.config file like this

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <appSettings>

        <add key="DefaultTimerIntervalInMinutes" value="2"/>

      </appSettings>

    </configuration>

    2.      I have a class AppConfigTest, like this (I have added System.configuration to References and use the System.Collections.Specialized)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Configuration;

    using System.Text;

    using System.Collections.Specialized;

    namespace AppConfigTest

    {

        class Program

        {

            public static void Main(string[] args)

            {

               NameValueCollection appSettings = ConfigurationManager.AppSettings;

               string defaultTimeIntervalInMinutes =appSettings["DefaultTimerIntervalInMinutes"];

               Console.WriteLine("Default timer interval in minutes : {0}", defaultTimeIntervalInMinutes);

            }

        }

    }

    3.      Create a unit test (the test project called TestProject1) for the method in class Program. Cope the app.config file to the Testproject1/bin/config, open the LocalTestRun.testrunconfig, add Directory like this \AppConfigTest\bin\Debug.

    using System.Configuration;

    using System.Collections.Specialized;

    ……

    [TestMethod()]

            public void MainTest()

            {

                string expectedValue = "2";

                NameValueCollection appSettings = ConfigurationManager.AppSettings;

                Assert.AreEqual(expectedValue, appSettings["DefaultTimerIntervalInMinutes"]);     

            }

    4.      Run the test, the result is passed.

     

    Even though I don’t create 2 folders and two App.config files like yours, I hope it is useful for you.

     

    Thanks,

    Captain


    When you help others, it's help yourself!
    • Marked as answer by Angela_HAC Tuesday, May 17, 2011 6:51 AM
    Tuesday, May 17, 2011 3:01 AM

All replies

  • Hi Angela_HAC

     

    Welcome to our MSDN forum.

    From your description, I think you could have a try like the following steps.

    1.      Add the app.config file in the deployment section on your unit tests before the tests are run.

    2.      Open the Local.testsettings file in Solution Items, select Deployment, then select Add Directory to select the location your added .config file, and then you could run the test to check it.

     

    A useful article, see Can a unit test project load the target applicaion’s app.config file?

    A perfect blog wrote by Nilesh Gule about Unit Test Application Configuration Settings. See http://nileshgule.blogspot.com/2010/09/unit-test-application-configuration.html

     

    Thanks,


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 13, 2011 8:44 AM
    Moderator
  • Visual Studio copies app.config to the output directory as MyTest.dll.config. Just override MyTest.dll.config with App_S01.config before running tests.

    There is no switch to force unit tests to load settings from App_S01.config instead of MyTest.dll.config. You have to override <dll>.config, or manually load settings from another configuration file in your code.


    My blog | My pet project
    Friday, May 13, 2011 12:50 PM
  • Hi,

    To run your test againsts differnt endpoints, you can create data driven test, please refer http://msdn.microsoft.com/en-us/library/ms182527(v=vs.80).aspx

    Thanks,

    Anuj

    Saturday, May 14, 2011 9:24 PM
  • For test purposes I added 2 folders to my solution

     Good\App.config - the test passes with this config

     Bad\App.config - the test fails with this config file

    I have create 2 test run config files: Good.testrunconfig and Bad.testrunconfig

     

    <?xml version="1.0" encoding="UTF-8"?>
    <TestRunConfiguration name="Good" id="82c3afc4-3b4b-42a7-b4d9-781dbdbcacb4" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
     <Description>This is a default test run configuration for a local test run.</Description>
     <Deployment>
     <DeploymentItem filename="Good\App.config" />
     </Deployment>
     <TestTypeSpecific />
    </TestRunConfiguration>
    
    I run this command mstest.exe /runconfig:Good.testrunconfig /testcontainer:MyTest.dll and it fails. What am I doing wrong?

     

    I have created another test project with this test:

       [TestMethod]
       public void TestAge()
       {
         string value = ConfigurationManager.AppSettings["age"];
         Assert.AreEqual(value, "21");
       }

    and when I run the mstest command with testrunconfig file I get the following error:

    Assert.AreEqual failed. Expected:<(null)>. Actual:<21>

    It seems like the App.config file from Good.testrunconfig is not found. Why is that?

    Monday, May 16, 2011 6:34 AM
  • Hi Angela_HAC,

     

    From your description, when you run it with Command Prompt, it is failed, could you tell us the detailed content about this error? How about just use one .testrunconfig file to check whether it was failed?

     

    Also, I think the .testrunconfig file isn’t in the same folder with .dll file, we often run it like this Mstest /testcontainer:C/…./TestProject1/bin/debug/mytest.dll /runconfig:LocalTestRun.testrunconfig.

     

    Note: Test run configurations have been replaced by test settings in Visual Studio 2010 Ultimate. See MSTest.exe Command-Line Options

     

    A useful thread, see Can I specify app.config file with MSTest in VS2008

     

    Thanks,


    When you help others, it's help yourself!
    Monday, May 16, 2011 9:01 AM
  • when I run from command prompt I specify the path to both the unit test dll and to the testrunconfig file

     

    Loading Good.testrunconfig...
    Loading bin/debug/PosUnitTests.dll...
    Starting execution...
    
    Results    Top Level Tests
    -------    ---------------
    Failed    MyTest.UnitTest.TestAge
    Failed    MyTest.UnitTest.TestName
    0/2 test(s) Passed, 2 Failed
    
    Summary
    -------
    Test Run Failed.
     Failed 2
     ---------
     Total 2
    Results file:  C:\..\..2011-05-16 11_55_59.trx
    Run Configuration: Good

     

    If I open the results file, there I see the error: Assert.AreEqual failed. Expected:<(null)>. Actual:<21> 

    From VS when I change the active run config, I get the same error.

    My idea is the put in the testrunconfig file the path to the app.config and when I run the test (VS with selected configuration or command prompt) I expect the config file mentioned in testrunconfig to be copied the the output directory and the tests run. But it seems the App.config never gets copied.

     


    Monday, May 16, 2011 9:16 AM
  • Yes, I agree with you, it seems that the App.config file doesn’t be called.

    We could narrow down this issue,

    1.      Create and Run this unit test without App.config file to check whether it has this error. If not we could make sure it is relation to the App.config file.

    2.      Open your .testrunconfig file in Solution Items, like Vicky’s suggestion step2, add the .config file to have a try.

    3.      How about use the suggested method create .dll.config files for each test assemlby in the thread I have provided?

    Best regard,

    Captain

     


    When you help others, it's help yourself!
    Monday, May 16, 2011 9:47 AM
  • 1.      Create and Run this unit test without App.config file to check whether it has this error. If not we could make sure it is relation to the App.config file.

    It does not run without an App.config file, because I read my test values from App.config
     [TestMethod]
      public void TestAge()
      {
       string value = ConfigurationManager.AppSettings["age"];
       Assert.AreEqual(value, "21");
      }
    
    2.      Open your .testrunconfig file in Solution Items, like Vicky’s suggestion step2, add the .config file to have a try
    I have already tried that. This is how my runtestconfig file looks like
    <?xml version="1.0" encoding="UTF-8"?>
    <TestRunConfiguration name="Good" id="1d118bd5-bb52-4c09-8cd9-230cfc19755f" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
     <Description>This is a default test run configuration for a local test run.</Description>
     <Deployment>
      <DeploymentItem filename="Good\App.config" />
      <DeploymentItem filename="Good\" />
     </Deployment> 
     <TestTypeSpecific />
    </TestRunConfiguration>
    
    3.      How about use the suggested method create .dll.config files for each test assemlby in the thread I have provided?
    I want to have different config files and using ms test to run the tests with the configuration I choose, based on the specified runtestconfig file.
    Monday, May 16, 2011 10:00 AM
  • I have replaced my App.config files with simple txt file (I read my data from a test file using SteamReader instead of ConfigurationManager.AppSettings) and now the tests pass.

    Is there a problem with App.config? Or it was the way I read the values causing the problem? 

    Monday, May 16, 2011 11:20 AM
  • Hi Angela_HAC,

    I have reappeared this issue like this link http://nileshgule.blogspot.com/2010/09/unit-test-application-configuration.html

    1.      I create a simple App.config file like this

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <appSettings>

        <add key="DefaultTimerIntervalInMinutes" value="2"/>

      </appSettings>

    </configuration>

    2.      I have a class AppConfigTest, like this (I have added System.configuration to References and use the System.Collections.Specialized)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Configuration;

    using System.Text;

    using System.Collections.Specialized;

    namespace AppConfigTest

    {

        class Program

        {

            public static void Main(string[] args)

            {

               NameValueCollection appSettings = ConfigurationManager.AppSettings;

               string defaultTimeIntervalInMinutes =appSettings["DefaultTimerIntervalInMinutes"];

               Console.WriteLine("Default timer interval in minutes : {0}", defaultTimeIntervalInMinutes);

            }

        }

    }

    3.      Create a unit test (the test project called TestProject1) for the method in class Program. Cope the app.config file to the Testproject1/bin/config, open the LocalTestRun.testrunconfig, add Directory like this \AppConfigTest\bin\Debug.

    using System.Configuration;

    using System.Collections.Specialized;

    ……

    [TestMethod()]

            public void MainTest()

            {

                string expectedValue = "2";

                NameValueCollection appSettings = ConfigurationManager.AppSettings;

                Assert.AreEqual(expectedValue, appSettings["DefaultTimerIntervalInMinutes"]);     

            }

    4.      Run the test, the result is passed.

     

    Even though I don’t create 2 folders and two App.config files like yours, I hope it is useful for you.

     

    Thanks,

    Captain


    When you help others, it's help yourself!
    • Marked as answer by Angela_HAC Tuesday, May 17, 2011 6:51 AM
    Tuesday, May 17, 2011 3:01 AM
  • Hello

    I have solved my problem using xml files instead of App.config. Now if I change the configuration and I run the tests both in VS or command line using MSTest I get the expected results.

    Thank you very much for your answers.
    Tuesday, May 17, 2011 6:52 AM
  • Hello,

     

    Glad to hear you have your issue resolved.

     

    Have a nice day.

     

    Best Regards,

    Captain


    When you help others, it's help yourself!
    Tuesday, May 17, 2011 6:59 AM
  • Not sure why that reply is marked as an answer. Test will pass even if you will skip step 3. App.config will by automatically copied to output directory - bin\debug.
    My blog | My pet project
    Tuesday, May 17, 2011 9:32 AM
  •  

    Yes, your suggestion is right, don’t need the Step3 to cope the App.config file to …\bin\debug, thanks for your help, PashaPash

     

    Thanks,

     

    Captain


    When you help others, it's help yourself!
    Wednesday, May 18, 2011 5:12 AM