NUnit 和 app.config 的问题
当我对数据库连接运行简单测试时,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,你可以。您需要确保您在测试中引用的任何配置实际上存在于测试项目的
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 itsapp.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.注意:如果不起作用,请尝试添加路径,例如 bin\Debug \ yourAssemblyName.dll.config
您的测试项目文件yourAssemblyName.nunit 将被更新。
是的,请确保测试项目中的 App.config 与您在测试中访问的内容匹配,即
App.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.
App.config
我想补充一点。 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
只要您的测试项目的配置与主项目相同,您的单元测试就应该仍然有效。
我建议在测试项目中使用预构建事件将应用程序的配置文件复制到测试项目。这样就不必维护两组配置。
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.
为什么需要单元测试来查看 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.
查看我的答案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
男士看一下:http://nunit.net/blogs/?p=9
正如他建议的那样,我将
MyProjectTests.dll.config
放在项目根目录中,一切正常。我的配置文件的一个示例是:
我使用的是简单的:
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:
And I'm using the simple:
ConfigurationManager.AppSettings["TestKey"];
当在运行测试的项目中使用配置文件时,必须遵循特定的命名约定。
配置文件名应该是带有 config 扩展名的程序集文件的名称。例如,用于运行
MyUnitTest.tests.dll
的配置文件必须命名为MyUnitTest.tests.dll.config
,并且它应该与位于同一目录中。 >MyUnitTest.nunit
我们也可以在预构建中配置它,如下所示
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 namedMyUnitTest.tests.dll.config
and it should be in the same directory as theMyUnitTest.nunit
Also we can configure this in prebuilds like below
我遇到了同样的情况,我尝试将 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.
And finally debugged my test and received the value from
App.config
file within my test project: