测试:我*想*测试 web.config

发布于 2024-08-24 05:05:10 字数 695 浏览 5 评论 0原文

我想对我的一个项目进行一些单元测试。这是一个网络项目,除了开发副本之外,只会运行该程序的一个副本。

我想编写一些使用 web.config 的单元测试。据我所知,通常情况下,测试人员会删除这个外部依赖项,因为他想在不依赖 web.config 保存某些值的情况下测试代码。

但是,我的项目中的 web.config 应该始终保存某些值,并且我希望进行一个单元测试,如果它们设置为无效值,该单元测试就会失败。例如,其中一个值是 SQL 连接字符串。

我想编写一个测试,从 web.config 读取连接字符串。我设想测试可以使用连接字符串连接到服务器,并且可能执行一个非常简单的命令,例如 SELECT system_user; 。如果命令成功执行并返回一些内容,则测试通过。否则,就会失败。我希望从我正在测试的项目中的 web.config 读取连接字符串。

当然,ConfigurationManager 通常不会在另一个项目中查找 web.config。我可以手动将 web.config 从原始项目复制到测试项目,但我必须在每次测试之前执行此操作,而且我无法指望其他人这样做。

如何让我的测试项目从另一个项目读取 web.config

I want to do some unit testing on one of my projects. This is a web project, and there will only be one copy of this program running aside from development copies.

I want to write some unit tests that will use the web.config. I understand that ordinarily, a tester would stub out this external dependency because he wants to test the code without the test depending on the web.config holding certain values.

However, the web.config in my project is supposed to always hold certain values and I want to have a unit test that will fail if they are set to invalid values. For example, one of the values is a SQL connection string.

I want to write a test that will read the connection string from the web.config. I envision that the test could connect to a server with the connection string and perhaps perform a very simple command like SELECT system_user;. If the command executes successfully and returns something the test passes. Otherwise, it fails. I want the connection string to be read from the web.config in the project I'm testing.

Of course, the ConfigurationManager will not ordinarily look for a web.config in another project. I could manually copy the web.config from the original project to the test project, but I would have to do that before every test and there is no way I could count on anyone else to do that.

How do I make my test project read the web.config from another project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

注定孤独终老 2024-08-31 05:05:10

听起来您正在尝试验证 web.config 中的设置,这是部署级别的问题,与单元测试不同。

单元测试告诉您您的核心逻辑是否按预期执行; 部署验证告诉您应用程序已正确安装和配置,并且可以安全使用。单元测试对开发人员有意义,部署验证对部署应用程序的最终用户或管理员有意义。

在这种情况下,我喜欢在我的应用程序中构建一个“系统控制台”。该控制台包含许多自诊断检查,例如:

  1. 确保连接字符串配置正确
  2. 确保任何第 3 方服务可用且正常运行
  3. 确保所有配置设置均有效且不会导致运行时错误(例如路径)存在,Web 用户帐户在需要时具有读/写访问权限等)

我强烈建议您考虑将此类配置和部署验证与单元测试套件分开。它不仅会简化您的工作(因为您不必从另一个项目加载配置文件),而且它也是客户非常非常喜欢的工具:)

It sounds like you are trying to validate settings in web.config, which is a deployment-level concern and is different from unit testing.

Unit testing tells you that your core logic is performing as expected; deployment verification tells you that the application was installed and configured properly and is safe to use. Unit tests are meaningful to developers, deployment verification is meaningful to the end user or administrator that is deploying the app.

In situation like this I like to build a "system console" into my apps. This console contains a number of self-diagnostic checks such as:

  1. Ensuring the connection string(s) are configured properly
  2. Ensuring that any 3rd party services are available and functioning
  3. Ensuring that all configuration settings are valid and won't cause runtime errors (e.g. paths exist, the web user account has read/write access where needed, etc)

I strongly recommend you consider separating this sort of configuration and deployment verification from your unit test suite. Not only will it simplify your work (because you won't have to load a config file from another project) but it's also the sort of tool that customers really, really like :)

权谋诡计 2024-08-31 05:05:10

您可以使用 ConfigurationManager.OpenXXX() 方法。

WebConfigurationManager 类专门有一个打开 web.config 文件的方法,我链接到的文档页面有更多代码示例。加载配置对象后,您可以探索它的部分和键。

var cfm = new ConfigurationFileMap("path/to/web.config");
var config = WebConfigurationManager.OpenMappedWebConfiguration(cfm);

You can load and explore other config files with the ConfigurationManager.OpenXXX() methods.

The WebConfigurationManager class specifically has a method for opening web.config files, and the documentation page I linked to has some more code examples. Once you have your configuration object loaded, you can explore it for sections and keys.

var cfm = new ConfigurationFileMap("path/to/web.config");
var config = WebConfigurationManager.OpenMappedWebConfiguration(cfm);
深海蓝天 2024-08-31 05:05:10

我问了一个类似的问题,您可能想看看:

我如何测试所有预期的 web.config 设置是否已定义?

我最终让它工作,但烦人的部分是我的源代码管理不断锁定配置文件复制过来了。您还可以将 web.config 重命名为 app.config,以便将其编译为非 Web 项目。

I asked a similar question that you might want to check out:

How do I test that all my expected web.config settings have been defined?

I ended up getting it working but the annoying part is my source control constantly locking the config file that is copied over. You can also rename the web.config to app.config so that it will compile into a non-web project.

音盲 2024-08-31 05:05:10

听起来就像你想用大锤压死一只蚊子。为什么不手动执行此操作,作为部署清单的一部分;有一个任务是手动确认连接字符串。

或者,如果您想自动化它,请编写一个程序来检查连接字符串,将其附加到您的持续集成服务器(假设您有一个),如果连接字符串错误,则构建失败。

使用单元测试来测试代码,而不是配置。

It sounds like you're trying to squash a mosquito with a sledgehammer. Why not do this manually, as part of the deployment checklist; have a task to manually confirm the connectionString.

Or if you want to automate it, write a program to check the connectionString, attach it to your Continuous Integration server (assuming you have one) and fail the build if the connectionString is wrong.

Use Unit Tests for what they're intended for testing code, not configuration.

岁月打碎记忆 2024-08-31 05:05:10

如果您想将网站中的原始 web.config 文件使用到您的单元测试项目而不进行复制,那么您可以修改 VS Local-Test-Settings

以下是在单元测试项目下使用 ASP.net 网站配置文件的分步过程。点击链接http://forums.asp.net/t/1454799.aspx/1< /a>

If you want to use original web.config file from your website to your Unit Testing project without copying it then you can Modify VS Local-Test-Settings.

Here is a step by step procedure to use ASP.net website configuration file under Unit Testing project. Follow the link http://forums.asp.net/t/1454799.aspx/1

偏爱你一生 2024-08-31 05:05:10

有一个名为 CheckMyConfig 的商业工具,用于验证 .NET 配置文件,它识别给定配置文件中的设置,并尝试验证它们。

可能的设置类型包括数据库连接字符串、文件、文件夹、IP 地址、主机名和 URL。

该工具允许您执行许多检查,包括打开数据库连接、访问文件夹、请求特定 URL 等。

有一个独立版本,但该工具还具有 Visual Studio 集成和一个可用于嵌入该工具的简单 API在您自己的应用程序中,按顺序
在应用启动时执行配置'健全性检查'

There is a commercial tool called CheckMyConfig for validating .NET config files, which identifies settings within a given config file, and attempts to validate them.

Possible setting types include database connection strings, files, folders, IP addresses, hostnames and URLs.

The tool allows you to perform a number of checks including opening database connections, accessing folders, requesting a particular URL etc.

There is a standalone version, but the tool also has Visual Studio integration and a simple API that you can use to embed the tool within your own apps, in order
to perform a config 'sanity check' at app startup time.

独留℉清风醉 2024-08-31 05:05:10

在您的单元测试项目中,添加 app.config 文件并添加您想要用于测试的 web.config 文件中的设置。

In your unit testing project, add an app.config file and add the settings from the web.config file that you would like to use for your tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文