Team System Developer Center > Visual Studio Team System Forums > Visual Studio Team System - Testing > Loading appsettings or custom settings from MSTest.exe
Ask a questionAsk a question
 

AnswerLoading appsettings or custom settings from MSTest.exe

  • Thursday, March 30, 2006 9:15 AMArturo72 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have a unit test (MyUnitTest.dll) that tests a class in another dll (MyClass.dll). My unit tests verify the code that loads the configuration settings. One test method looks like this,

    [TestMethod]

    [DeploymentItem("MyUnitTest.dll.config")]

    public void ReadFromConfig() {

    MyConfiguration configuration = MyConfiguration.Configuration;

    Assert.AreEqual("testvalue", configuration.CustomList[0]);

    }

    When I run this from VS IDE (I specify "MyUnitTest.dll.config" as a Deployment file in localtestrun.testrunconfig), the test runs successfully.

    When I run the same test from MSTest.exe using,

    MSTest.exe /testcontainer:MyUnitTest.dll

    The tests fails because it cannot load the configuration file. I debugged the tests from the MSTest process and even calls to CustomManager.AppSetting return nothing. I am sure that the configuration file as well as the binaries are copied into the TestResults\<timestamp>\Out folder.

    Why the application settings are not being loaded?

    Thanks,

     

    Arturo

Answers

  • Monday, April 03, 2006 9:51 PMMichael Koltachev - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Also please make sure that from command line:
    - when you run the test AppDomain.CurrentDomain.SetupInformation.ConfigurationFile is not empty
    - when you run the test .config file exists on disk (you can do MessageBox.Show or Debug.Fail)
    - In the beginning of the test do: ConfigurationManager.RefreshSection("testSettings")
    - Create an ordered test, add all your tests in it with problem test first. This will make sure that no other tests affect this test.

    Also note that tests are executed in different order when running from IDE and command line.

    Thank you,
    Michael Koltachev
    VSTS

All Replies

  • Thursday, March 30, 2006 6:44 PMDavid Gorena Elizondo - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Arturo

    If your tests make use of the testrunconfig file, you will have to specify the testrunconfig file when running your tests from the commandline as well; try something like this:

    MSTest.exe /testcontainer:MyUnitTest.dll /runconfig:localtestrun.testrunconfig

    Thanks,
    David Gorena Elizondo
    [MSFT] VSTS

  • Friday, March 31, 2006 12:13 AMJames Su - MSFTMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    David already showed the right command line to run the test in your case. I just want to add to it that you don't need to specify your deployment item (MyUnitTest.dll.config) in both test run configuration and as the test attribute.

    If you need this config file for all the tests in the run, then you want to add it to the test run configuration file. Otherwise, if only one test needs it, you can add it as the attribute of the test (per test deployment).

    So to solve the issue you had, you can either specify an absolute path to MyUnitTest.dll.config as the DeploymentItem attribute (then you just need to run the test without specifying the test run configuration file), or you can add it to the run configuration file and then specify the run configuration file on the command line like David pointed out.

    Please notice that if you specify relative path in the DeploymentItem attribute (like you did in your source code), the path is relative to the current working directory.

    Thanks,

    James

    VSTS

  • Friday, March 31, 2006 1:24 AMArturo72 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    David,

    My tests will not have localtestrun.testrunconfig. I tested with with the testunconfigfile I used while debugging in VS, and ran it as you specified. And it still doesn't work.

    I even checked the config file, and it has an abosolute path to my config file (not a relative path).

    James,

    I put a absolute path as a DeploymentItem attribute and it still doesn't work. The problem is not that the file is not being copied when I run my test, MyUnitTest.dll.config is in my working directory,and it is being deployed with the rest of the files. I have other files deployed (dlls, text files) that are being deployed and accessed by my tests. It only fails when trying to access the file from using the ConfigutationManager class. I think this has to do on how MSTest creates the AppDomain to run the tests in. Is there a way for me to debug how the config file is being searched?

    Thanks,

    Arturo

  • Monday, April 03, 2006 6:21 PMMichael Koltachev - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Arturo, can you please zip & send me your solution to koltachev@hotmail.com(donotspam) [remove (donotspam)]?

    Thank you,
    Michael Koltachev
    VSTS

  • Monday, April 03, 2006 9:51 PMMichael Koltachev - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Also please make sure that from command line:
    - when you run the test AppDomain.CurrentDomain.SetupInformation.ConfigurationFile is not empty
    - when you run the test .config file exists on disk (you can do MessageBox.Show or Debug.Fail)
    - In the beginning of the test do: ConfigurationManager.RefreshSection("testSettings")
    - Create an ordered test, add all your tests in it with problem test first. This will make sure that no other tests affect this test.

    Also note that tests are executed in different order when running from IDE and command line.

    Thank you,
    Michael Koltachev
    VSTS

  • Tuesday, April 04, 2006 12:09 AMArturo72 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks Michael! The solution was to call ConfigurationManager.RefreshSection("testSettings") before starting each tests that reads the configuration files.

    ConfigurationManager testing recommendations (or things I wish I knew before posting this),

    • If you have tests that modify the configuration file, you need to call ConfigurationManager.RefreshSection(sectionName) to reload the configuration file.
    • Also, it is good to know that you don't need to add the configuration file as a DeploymentItem (Thanks James).
    • The name of the configuration file should be <test-dll>.config.
    • When you debug the MSTest.exe process, you can only breakpoint in your tests if you specify the /noisolation option. When you specify the /noisolation option, configuration manager tests will fail because MSTest.exe.config is loaded instead of <test-dll>.config.
    • The property AppDomain.CurrentDomain.SetupInformation.ConfigurationFile returns the name of the configuration file loaded by the current domain.

    Thanks to all for your help!

    Arturo