visual-studio NUnit 和 app.config 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2409208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:38:23  来源:igfitidea点击:

The problem with NUnit and app.config

visual-studionunitapp-configconfigurationmanager

提问by Mike

When i run a simple test on connection to DB check i receive an error in NUnit:

当我对连接到 DB 检查运行一个简单的测试时,我在 NUnit 中收到一个错误:

[Test]
public void TestConn()
{
    string  connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString);
    connection.Open();
    Assert.AreEqual(ConnectionState.Open, connection.State);
    connection.Close();
 }

System.NullReferenceException : Object reference not set to an instance of an object.

System.NullReferenceException:未将对象引用设置为对象的实例。

on line :

在线的 :

connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;

Can i use ConfigurationManager in tests?

我可以在测试中使用 ConfigurationManager 吗?

回答by Oded

Yes, you can. You need to be sure that any configuration you are referencing in your tests actually exist in the app.configof the test project.

是的你可以。您需要确保您在测试中引用的任何配置实际上都存在于app.config测试项目中。

In other words, the project where your test is in, does not have a connection string "FertigungRead"defined in its app.config.

换句话说,您的测试所在的项目"FertigungRead"在其app.config.

One way to do this is to add the app.configof the system under test to the test project as a link, this way any changes happen on both projects.

一种方法是将app.config被测系统作为链接添加到测试项目,这样任何更改都会发生在两个项目上。

回答by ivan_d

  1. Go to NUnit/Project/Edit...
  2. In Configuration Property panel go to Configuration File Name
  3. Put there yourAssemblyName.dll.config
  1. 转到 NUnit/项目/编辑...
  2. 在配置属性面板中,转到配置文件名
  3. yourAssemblyName.dll.config放在那里

NB: if does not work try to add path to it, e.g. bin\Debug\ yourAssemblyName.dll.config

注意:如果不起作用尝试添加路径,例如 bin\Debug\ yourAssemblyName.dll.config

Your test project file yourAssemblyName.nunit will be updated.

您的测试项目文件yourAssemblyName.nu​​nit 将被更新。

And yes, be sure App.config in your test project match to what you access in test, i.e.

是的,请确保您的测试项目中的 App.config 与您在测试中访问的内容相匹配,即

[Test]
public void TestConn()
{
   var sss = ConfigurationManager.AppSettings["TestKey"];
}

App.config

应用配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestKey" value="testKeyValue"/>
  </appSettings>
</configuration>

回答by amarnath chatterjee

I would like to add a point. There is a note in documentation of nunit, and depending upon usage scenario config file naming and placement should be done. I was stuck on this issue for a while.

我想补充一点。nunit 的文档中有一个注释,应该根据使用场景配置文件命名和放置。我被这个问题困住了一段时间。

Documentation says :

文档说:

If a single assembly is being loaded, then the configuration file is given the name of the assembly file with the config extension.For example, the configuration file used to run nunit.tests.dll must be named nunit.tests.dll.config and located in the same directory as the dll.

如果正在加载单个程序集,则配置文件被赋予程序集文件的名称,并带有 config 扩展名。例如,用于运行 nunit.tests.dll 的配置文件必须命名为 nunit.tests.dll.config 并与 dll 位于同一目录中。

If an NUnit project is being loaded, the configuration file uses the name of the project file with the extension changed to config.For example, the project AllTests.nunit would require a configuration file named AllTests.config, located in the same directory as AllTests.nunit. The same rule is followed when loading Visual Studio projects or solutions.

如果正在加载 NUnit 项目,则配置文件使用项目文件的名称并将扩展名更改为 config。例如,项目 AllTests.nunit 需要一个名为 AllTests.config 的配置文件,该文件与 AllTests.nunit 位于同一目录中。加载 Visual Studio 项目或解决方案时遵循相同的规则。

http://www.nunit.org/index.php?p=configFiles&r=2.2.10

http://www.nunit.org/index.php?p=configFiles&r=2.2.10

回答by Richard Ev

Your unit tests should still work as long as you have the same configuration for your test project as for your main project.

只要您的测试项目与主项目的配置相同,您的单元测试就应该仍然有效。

I'd suggest using a pre-build event in your test project to copy your application's configuration file over to the test project. This saves having to maintain two sets of configuration.

我建议在测试项目中使用预构建事件将应用程序的配置文件复制到测试项目。这样就不必维护两组配置。

copy $(SolutionDir)path-to-main-project\Web.config $(ProjectDir)App.config

复制 $(SolutionDir)path-to-main-project\Web.config $(ProjectDir)App.config

回答by tvanfosson

Why do you need a unit test to see if SqlConnection works? You should test your code, not Microsoft's. I don't really see the point in checking if the connection string is correct either in your unit tests. The configuration used by the unit tests isn't the same as what will be used by your production code.

为什么需要单元测试来查看 SqlConnection 是否有效?你应该测试你的代码,而不是微软的。在您的单元测试中,我真的不明白检查连接字符串是否正确的意义。单元测试使用的配置与您的生产代码将使用的配置不同。

In general, though, if you need some configuration data for unit tests, create an app.config file in the test project. Populate the appSettings and connectionStrings elements, etc. with appropriate values for your test environment. Don't bother testing whether ConfigurationManager or SqlConnection works, though. You'll just be creating code that you have to maintain, but that doesn't actually verify any of the production code you are writing.

但是,一般而言,如果您需要一些单元测试的配置数据,请在测试项目中创建一个 app.config 文件。使用适合您的测试环境的值填充 appSettings 和 connectionStrings 元素等。不过,不要费心测试 ConfigurationManager 或 SqlConnection 是否有效。您只会创建必须维护的代码,但这实际上并不会验证您正在编写的任何生产代码。

回答by skyfoot

See my answer nunit and configsYou need to tell nunit what the name of the config file is. it looks for namespace.config by default it seams

看我的回答nunit 和 configs你需要告诉 nunit 配置文件的名字是什么。默认情况下,它会查找 namespace.config 它会接缝

回答by Custodio

Man give a look at: http://nunit.net/blogs/?p=9

男人看看:http: //nunit.net/blogs/?p=9

As him suggest I put a MyProjectTests.dll.configin project root and everything works.

正如他所建议的那样,我MyProjectTests.dll.config在项目根目录中放入了一个,一切正常。

An example of my config file is:

我的配置文件的一个例子是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <add key="TestKey" value="Ok!"/>
</appSettings>
</configuration>

And I'm using the simple: ConfigurationManager.AppSettings["TestKey"];

我使用的是简单的: ConfigurationManager.AppSettings["TestKey"];

回答by manjula k m

When a configuration file is used in the project in which a test is run, specific naming conventions must be followed.

在运行测试的项目中使用配置文件时,必须遵循特定的命名约定。

The configuration file name should be the name of the assembly file with the config extension. For example, the configuration file used to run MyUnitTest.tests.dllmust be named MyUnitTest.tests.dll.configand it should be in the same directory as the MyUnitTest.nunit

配置文件名应该是带有配置扩展名的程序集文件的名称。例如,用于运行的配置文件MyUnitTest.tests.dll必须命名MyUnitTest.tests.dll.config,并且应该与运行的配置文件在同一目录中MyUnitTest.nunit

Also we can configure this in prebuilds like below

我们也可以在预构建中配置它,如下所示

copy $(SolutionDir)path-to-main-project\Web.config $(ProjectDir)App.config 

回答by Ben

I was in the same situation and I tried to add app.config to NUnit project but the project couldn't identify it. The tricky workaround for me was to create a class library that contains an app config and turn in into my test project. At the end of the day, all NUnit and its adapter are NuGet packages that can be added for tests to a class library and tests work perfectly fine ran by Visual Studio and Resharper.

我处于同样的情况,我尝试将 app.config 添加到 NUnit 项目,但该项目无法识别它。对我来说,棘手的解决方法是创建一个包含应用程序配置的类库,然后转入我的测试项目。归根结底,所有 NUnit 及其适配器都是 NuGet 包,可以将这些包添加到类库中进行测试,并且测试工作由 Visual Studio 和 Resharper 完美运行。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

And finally debugged my test and received the value from App.configfile within my test project:

最后调试了我的测试并从App.config我的测试项目中的文件中接收到值:

enter image description here

在此处输入图片说明