C#:使用不同的配置文件运行每个单元测试 [英] C#: Run each unit test with different configuration file

查看:49
本文介绍了C#:使用不同的配置文件运行每个单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个插件以在任何应用程序中使用,插件的配置是通过使用 web.config 或 app.config 文件完成的.

I'm currently developing a plugin to use in any application, and the configuration of the plugin is done through the usage of either a web.config or an app.config file.

现在,我想对我的插件进行单元测试.我想在各种条件下测试不同的行为:

Now, I want to unit test my plugin. I want to test the different behaviour in various conditions:

例如:

  1. 只能将 1 个单项标记为默认".
  2. 可以在插件中注册一组提供者,但名称必须是唯一的,否则会引发特定异常.

我的问题:

我有一个单元测试项目,我想用另一个应用程序配置文件测试每个方法,这可能吗?

I have a unit test project and I want to test each method with another application configuration file, is this possible?

我已尝试从 ExeConfiguration 读取文件,但无法正常工作.

I've tried reading the file from ExeConfiguration, but I don't get it to work.

感谢您的反馈.

氪,

推荐答案

我的建议是模拟与配置文件的集成,而不是直接使用它.这种方法提供了更大的灵活性,并且无需担心多个配置文件.您可以使用模拟框架,也可以在获取配置值的代码之上创建自己的抽象层.

My recommendation is to mock the integration with the config file instead of using it directly. This approach offers much more flexibility and removes the need to worry about multiple config files. You can either use a mocking framework or create you own layer of abstraction on top of the code that fetches the config values.

这篇关于C#:使用不同的配置文件运行每个单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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