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
- Dot.Net Framework: Microsoft.NET Framework version 4.7.0
- OS: window 10
- IDE: Visual studio 2019
- Use MS TEST framework for unit testing
- MS Test.TestAdapter (Version) 2.1.0
- Microsoft.NET.Test.Sdk (Version) 16.5.0
- System.Configuration.ConfigurationManager (Version) 4.7.0
- 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