单元测试中读取配置文件问题

来源:互联网 发布:淘宝上的苹果旗舰店 编辑:程序博客网 时间:2024/06/05 15:59

在新建的单元测试中,如果被测试的方法有对配置文件(App.conf)的读取,如:
public static readonly string ConnectionString = ConfigurationSettings.AppSettings["SQLProfileConnString"].ToString();
将不能得到正常值,值为null。解决方法是为测试项目添加应用程序配置文件:

然后写入配置,如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SQLProfileConnString" value="Pooling='false';server=127.0.0.1,1433;database=pro_db;user id=pro;password=pro;" />
  </appSettings>
</configuration>

原创粉丝点击