我有多个NUnit测试,我希望每个测试都使用特定的app.config文件。 有没有办法在每次测试之前将配置重置为新的配置文件?用nunit重新加载app.config
10
A
回答
8
尝试:
每个测试/* Usage
* using(AppConfig.Change("my.config")) {
* // do something...
* }
*/
public abstract class AppConfig : IDisposable
{
public static AppConfig Change(string path)
{
return new ChangeAppConfig(path);
}
public abstract void Dispose();
private class ChangeAppConfig : AppConfig
{
private bool disposedValue = false;
private string oldConfig = Conversions.ToString(AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE"));
public ChangeAppConfig(string path)
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
typeof(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, 0);
}
public override void Dispose()
{
if (!this.disposedValue)
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", this.oldConfig);
typeof(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, 0);
this.disposedValue = true;
}
GC.SuppressFinalize(this);
}
}
}
0
如果问题是您针对不同的测试用例集需要不同的配置,那么您可以为每个测试用例配置不同的测试项目。然后一次运行一个测试项目。
+0
一个项目? – apollodude217 2010-07-15 20:08:54
0
I answered a similar question for Powershell。同样的技术也应该在这里工作,只需调用在测试开始时的以下内容:
System.AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath)
编辑:其实看起来这是一个比较复杂编译EXE中 - 你需要做的something like this为了得到该配置重新加载。
相关问题
- 1. 如何重新加载/刷新app.config?
- 2. 使用Nunit和的app.config
- 3. NUnit的和App.config中
- 4. 使用Moq,NUnit和实体框架重新加载实体
- 5. 重新缓存App.Config
- 6. 重新加载div重新加载XML
- 7. 重新加载不重新加载
- 8. 重新加载Java Applet重新加载
- 9. 重新加载
- 10. 如何使用IPython重新加载和自动重新加载?
- 11. app.config刷新部分不重新载入appsettings部分
- 12. 将app.config加载到AppDomain中
- 13. 错误的app.config加载
- 14. 如何加载App.config文件?
- 15. 在运行时加载App.Config
- 16. 用javascript重新加载flash
- 17. 用jquery重新加载div
- 18. 用addOperationWithBlock重新加载TableView
- 19. NUnit延迟加载TestCaseSource
- 20. Nunit gui未找到app.config文件
- 21. 重新加载searchResultsTableView
- 22. 重新加载UIModalViewControllers
- 23. 重新加载tabbarviewcontroller
- 24. 重新加载UITableView
- 25. 重新加载AQGridview
- 26. 重新加载UITableView
- 27. 重新加载AVMutableComposition
- 28. 重新加载XPPageSelector
- 29. 重新加载UINavigationItem
- 30. 重新加载searchResultsTableView
这似乎工作完美。你会如此善意地解释它实际上做了什么? :-) – Karsten 2009-06-09 15:22:45