NUnit 和 app.config 的问题

发布于 2024-08-24 19:37:42 字数 636 浏览 10 评论 0原文

当我对数据库连接运行简单测试时,我在 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:未将对象引用设置为对象的实例。

在线:

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

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

When i run a simple test on connection to DB check i receive an error in 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.

on line :

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

Can i use ConfigurationManager in tests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

暮倦 2024-08-31 19:37:42

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

换句话说,您的测试所在的项目在其 app.config 中没有定义连接字符串 "FertigungRead"

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

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

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

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

阿楠 2024-08-31 19:37:42
  1. 转到 NUnit/Project/Edit...
  2. 在配置属性面板中转到配置文件名
  3. 放置 yourAssemblyName.dll.config

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

您的测试项目文件yourAssemblyName.nunit 将被更新。

是的,请确保测试项目中的 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>
  1. Go to NUnit/Project/Edit...
  2. In Configuration Property panel go to Configuration File Name
  3. Put there yourAssemblyName.dll.config

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

Your test project file yourAssemblyName.nunit will be updated.

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

[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>
似狗非友 2024-08-31 19:37:42

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

文档说:

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

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

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

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.

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.

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.

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

哥,最终变帅啦 2024-08-31 19:37:42

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

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

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

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

一绘本一梦想 2024-08-31 19:37:42

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

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

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.

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.

一念一轮回 2024-08-31 19:37:42

查看我的答案nunit 和配置
您需要告诉 nunit 配置文件的名称是什么。它默认查找namespace.config,它接缝

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

歌枕肩 2024-08-31 19:37:42

男士看一下:http://nunit.net/blogs/?p=9

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

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

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

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

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

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

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"];

甲如呢乙后呢 2024-08-31 19:37:42

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

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

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

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

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.dll must be named MyUnitTest.tests.dll.config and it should be in the same directory as the MyUnitTest.nunit

Also we can configure this in prebuilds like below

copy $(SolutionDir)path-to-main-project\Web.config $(ProjectDir)App.config 
乄_柒ぐ汐 2024-08-31 19:37:42

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

输入图片此处描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

最后调试了我的测试并从 App.config 文件:

在此处输入图像描述

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.

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.config file within my test project:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文