【问题标题】:Not able to read the values from ‘App.config’ file in the Unit testing project (Nunit)无法从单元测试项目 (Nunit) 中的“App.config”文件中读取值
【发布时间】:2020-10-30 13:35:38
【问题描述】:

我在 Visual Studio 中创建了一个“nunit”项目,其中包含一个简单的测试。没有可用的默认“App.config”文件,因此我创建了一个“App.config”文件。我已从其“属性”选项中将此文件标记为“始终复制”。

我尝试从“config”文件中读取值有两种方式“ConfigurationManager.AppSettings”和“system.configuration.configurationmanager.AppSettings”。但是,它显示的是空值(空值)

请看我的代码。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Text;
using log4net;
using NUnit.Framework;
using NUnitTestProject1.Config;
using NUnitTestProject1.Helper;

namespace NUnitTestProject1.AutomationTest
{
   [TestFixture, Order(3)]
    public class ReadDataThoughExcel
    {

        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

       [Test, Order(1)]
        public void IsUserReadDataThroughExcel() {

    string data1 = ConfigurationManager.AppSettings["name"];
    Console.Write("Print data "+data1);
    string data2 = system.configuration.configurationmanager.AppSettings["name"];
    Console.Write("Print data "+data2);
  }
 }
}

like so

配置文件代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <ApplicationSettings>
        <add key="name" value="Configuration Example Project"/>
    </ApplicationSettings>
</configuration>

like so

我的项目的配置详情:

  1. Dot.Net Framework: Microsoft.NET Framework 4.7.0 版
  2. 操作系统: 窗口 10
  3. IDE:Visual Studio 2019
  4. 使用 Nunit 框架进行单元测试
  5. NUnit3TestAdapter(版本)3.17.0
  6. Microsoft.NET.Test.Sdk(版本)16.5.0
  7. System.Configuration.ConfigurationManager(版本)4.7.0
  8. Selenium.Support(版本)3.141.0

注意:另外,我尝试通过右键单击“依赖项”选项将“system.configuration”的引用添加到我的项目中(在 Visual Studio 的项目名称中显示) ,但是我无法在“类型库”选项中看到选项“system.configuration”。

【问题讨论】:

    标签: c# selenium frameworks dependencies nunit


    【解决方案1】:

    您正在测试的应用程序可能已经有一个配置文件。此外,NUnit 本身可能有它自己的配置文件。为了避免冲突,NUnit 一直要求为 tests 使用的配置文件专门命名。

    具体来说,如果包含您的测试的程序集名为 mytests.dll,那么 NUnit 将在您的 bin 目录中查找名为 mytests.config 的配置。

    有关详细信息,请参阅我的博客文章http://charliepoole.org/technical/how-nunit-finds-config-files.html。 [如此旧的帖子仍然有用是不寻常的,但在这种情况下,NUnit 的工作方式没有任何改变。]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-15
      • 2018-12-11
      • 2011-08-09
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2011-03-27
      • 2011-05-05
      相关资源
      最近更新 更多