vendredi 6 novembre 2020

Not able to read value from app.config file in MS Test

I have created one ‘MS Test’ project in a visual studio which contains a simple one test case. There is no default ‘App.config’ file is available, so I have created one ‘App.config’ file. Also, I have tried to marked them ‘copy always’ from its properties option. I have tried to read the value from the config file in two ways, however it is giving always ‘null’ value.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Configuration;

namespace UnitTestProject4
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {

          string data2=ConfigurationManager.AppSettings.Get("MyName");
          Console.WriteLine("data2 " + data2);

        }
    }
}

like so

My App.Config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions" />
    <appsettings>
        <add key="MyName" value="Piyusha Naphade" />
    </appsettings>
</configuration>

like so

My system configuration

  1. Dot.Net Framework: Microsoft.NET Framework version 4.7.0
  2. OS: window 10
  3. IDE: Visual studio 2019
  4. Use MS TEST framework for unit testing
  5. MS Test.TestAdapter (Version) 2.1.0
  6. Microsoft.NET.Test.Sdk (Version) 16.5.0
  7. System.Configuration.ConfigurationManager (Version) 4.7.0
  8. MS Test.TestFramework 2.1.0

Note: Also, I have tried to add the reference of the ‘system.configuration’ to my project by right clicking on the 'Dependency' option (showing inside the project name in visual studio), however I am not able to see the option ‘system.configuration’ in the 'Type Libraries' option

Aucun commentaire:

Enregistrer un commentaire