在单元测试中修改应用程序设置 [英] Modifying application settings in unit tests

查看:316
本文介绍了在单元测试中修改应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库我想使用Microsofts单元测试框架进行单元测试。我想要测试的一些类使用应用程序设置配置。这些设置在具有应用程序范围和合适的默认值的 Settings.settings 文件中定义。当应用程序使用库时,这些设置可以在 App.Config 文件中覆盖。如果没有使用默认值。这正是我想要的。



在我的一些测试用例中,我想测试设置值的特殊组合,但我不知道如何更改值看到的类从测试中的单元测试代码。



在我的库类中,我访问的设置如下:

  var mySetting1 = Settings.Default.MySetting1; 
var mySetting2 = Settings.Default.MySetting2;

如何在测试类访问设置之前在单元测试中修改这些设置?通过单元测试访问内部设置类不能解决问题,因为设置具有应用范围,并且是设置类上的只读属性。

解决方案

在spelunking到 ApplicationSettingsBase 和相关类后,我想出了这个解决方案我的问题。



代码生成的设置类是类库项目的内部,它必须可以被单元测试项目访问。在类库项目中将 [assembly:InternalsVisibleTo(UnitTestAssemblyName)] 属性添加到 AssemblyInfo.cs / p>

当访问值时,设置从设置类的属性中延迟加载。第一步是做一个设置的虚拟读取,强制这个延迟负载。当单元测试要避免在一个测试中更改的设置值影响另一个测试,因此在延迟加载设置之前需要重置设置。这可以使用 Reload()方法来完成。此代码放置在测试初始化​​方法中:

  Settings.Default.Reload(); 
var dummy = Settings.Default.MySetting1;

基础值现在存在,可以在每个测试方法中设置。记住使用正确的类型作为代码生成的getter将执行转换:

  Settings.Default.PropertyValues [MyStringSetting1] .PropertyValue =Foobar; 
Settings.Default.PropertyValues [MyDoubleSetting2]。PropertyValue = 3.1416D;


I have a class library I want to unit test using Microsofts unit test framework. Some of the classes I want to test are configured using application settings. These settings are defined inside the Settings.settings file having application scope and suitable default values. When the library is used by the application these settings can be overriden in the App.Config file. If not the default values are used. That's exactly how I want it to be.

In some of my test cases I want to test special combinations of setting values but I don't know how to change the values seen by the class under test from the unit test code. These settings will always have their default value loaded from the attributes of the code generated class.

In my library class I access the settings like this:

var mySetting1 = Settings.Default.MySetting1;
var mySetting2 = Settings.Default.MySetting2;

How do I modify these settings in an unit test before the setting is accessed by the class under test? Making the internal settings class accessible by the unit test does not solve the problem as the settings have application scope and are read-only properties on the settings class.

解决方案

After spelunking into the ApplicationSettingsBase and associated classes I've come up with this solution to my problem. Not particular beautiful, but it certainly gets the job done.

The code generated settings class is internal to the class library project and it has to be accessible to the unit test project. Add the [assembly: InternalsVisibleTo("UnitTestAssemblyName")] attribute to AssemblyInfo.cs in the class library project.

The settings are lazy loaded from the attributes on the settings class when a value is accessed. First step is to do a "dummy" read of a setting to force this lazy load. When unit testing you want to avoid settings values changed in one test to affect another hence it is necessary to "reset" the settings before lazy loading them. That can be done using the Reload() method. This code is placed in the test initialize method:

Settings.Default.Reload();
var dummy = Settings.Default.MySetting1;

The underlying values now exist and can be set in each test method. Remember to use the correct type as the code generated getters will do a cast:

Settings.Default.PropertyValues["MyStringSetting1"].PropertyValue = "Foobar";
Settings.Default.PropertyValues["MyDoubleSetting2"].PropertyValue = 3.1416D;

这篇关于在单元测试中修改应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆