使用 NUnit 测试网站项目

发布于 2024-09-06 10:44:26 字数 583 浏览 1 评论 0原文

我是网络开发新手,有以下问题

我有网站项目。我在 App_Code 文件夹中有一个 datacontext 类,其中包含使用数据库的方法(那里也存在 dbml 模式)和不直接干扰数据库的方法。我想使用 NUnit 测试这两种方法。

由于 Nunit 使用 .dll 或 .exe 中的类,我知道我需要将整个项目转换为 Web 应用程序,或者将我想要测试的所有代码(即:App_Code 的全部内容)移动到一个类库项目并在网站项目中引用该类库项目。

如果我选择将方法移至单独的 dll,问题是如何测试那些与数据库一起使用的方法? :

  • 我需要创建一个连接吗 运行前在“setup”方法中添加db 每种方法?在这种情况下不需要运行 web appl,这是正确的吗?

  • 或者我需要在期间运行此类测试 网站的运行时间 连接已建立?在这种情况下如何设置项目和 Nunit?

  • 或另一种方式..

其次,如果一种方法依赖于我的.config文件中的某些设置,例如某些网络凭据或smtp设置,那么测试此类的方法是什么方法?

我将非常感谢任何帮助! 越具体越好。 谢谢。

i'm new in web dev and have following questions

I have Web Site project. I have one datacontext class in App_Code folder which contains methods for working with database (dbml schema is also present there) and methods which do not directly interfere with db. I want to test both kind of methods using NUnit.

As Nunit works with classes in .dll or .exe i understood that i will need to either convert my entire project to a Web Application, or move all of the code that I would like to test (ie: the entire contents of App_Code) to a class library project and reference the class library project in the web site project.

If i choose to move methods to separate dll, the question is how do i test those methods there which are working with data base? :

  • Will i have to create a connection to
    db in "setup" method before running
    each of such methods? Is this correct that there is no need to run web appl in this case?

  • Or i need to run such tests during
    runtime of web site when the
    connection is established? In this case how to setup project and Nunit?

  • or some another way..

Second if a method is dependent on some setup in my .config file, for instance some network credentials or smtp setup, what is the approach to test such methods?

I will greatly appreciate any help!
The more it's concrete the better it is.
Thanks.

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

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

发布评论

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

评论(1

友谊不毕业 2024-09-13 10:44:26

一般来说,您应该模拟数据库而不是真正连接到它进行单元测试。这意味着您提供了返回预设结果的虚假数据访问类实例。通常,您会使用模拟框架,例如 MoqRhino 为你做这种事情,但很多人也只是编写自己的一次性类来服务相同的目的。您的测试不应依赖于生产网站的配置设置。

这样做的原因有很多,但主要是将测试与实际的数据库实现分开。您所描述的将产生非常脆弱的测试,需要大量维护。

请记住,单元测试是为了确保一小部分代码能够正常工作。如果您需要测试一个复杂的操作是否自上而下地工作(即用户单击某个内容、从数据库获取数据、返回数据并更新 UI 的步骤之间的所有操作),那么这称为集成测试。如果您需要进行完整的集成测试,通常建议您有一个生产环境的副本 - 我的意思是完全副本,相同的硬件、软件,一切 - 您可以针对它运行集成测试。

Generally, you should be mocking your database rather than really connecting to it for your unit tests. This means that you provide fake data access class instances that return canned results. Generally you would use a mocking framework such as Moq or Rhino to do this kind of thing for you, but lots of people also just write their own throwaway classes to serve the same purpose. Your tests shouldn't be dependent on the configuration settings of the production website.

There are many reasons for doing this, but mainly it's to separate your tests from your actual database implementation. What you're describing will produce very brittle tests that require a lot of upkeep.

Remember, unit testing is about making sure small pieces of your code work. If you need to test that a complex operation works from the top down (i.e. everything works between the steps of a user clicking something, getting data from a database, and returning it and updating a UI), then this is called integration testing. If you need to do full integration testing, it is usually recommended that you have a duplicate of your production environment - and I mean exact duplicate, same hardware, software, everything - that you run your integration tests against.

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