测试与NUnit的app.config文件股文件、测试、app、NUnit

2023-09-02 11:50:15 作者:时间的新欢

当你们是单元测试依赖值从app.config文件的应用程序?你怎么测试,这些值正确读取和您的程序如何反应,进入了一个配置文件不正确的值?

When you guys are unit testing an application that relies on values from an app.config file? How do you test that those values are read in correctly and how your program reacts to incorrect values entered into a config file?

这是荒谬的必须修改NUnit的应用程序的配置文件,但我无法读取从我想要测试的app.config中的值。

It would be ridiculous to have to modify the config file for the NUnit app, but I can't read in the values from the app.config I want to test.

编辑:我想我应该澄清也许。我不担心ConfigurationManager中不能读取的值,但我关心的是如何测试我的程序反应读取值。

I think I should clarify perhaps. I'm not worried about the ConfigurationManager failing to read the values, but I am concerned with testing how my program reacts to the values read in.

推荐答案

我一般隔离外部的依赖就像读在自己的门面级的配置文件非常小的功能。在测试中,我可以创建,而不是真正的配置文件中这个类实现和使用的模拟版本。你可以创建自己的样机的或使用类似MOQ或犀牛制品的框架这一点。

I usually isolate external dependencies like reading a config file in their own facade-class with very little functionality. In tests I can create a mock version of this class that implements and use that instead of the real config file. You can create your own mockup's or use a framework like moq or rhino mocks for this.

这样,你可以很容易地尝试一下你的code不同的配置价值,而无需编写复杂的测试,首先编写XML配置文件。在code,读取配置通常是如此简单,它需要很少的测试。

That way you can easily try out your code with different configuration values without writing complex tests that first write xml-configuration files. The code that reads the configuration is usually so simple that it needs very little testing.