如何使用 ASP.NET MVC 和单元测试组织代码和测试
因此,我硬着头皮尝试开始使用 asp.net MVC、单元测试和 TDD。
我对所涉及的概念有一个模糊的理解,也就是说有点超出了“Hello World”的水平,但仍然很新。我已经准备好迎接路面了,但是 我发现自己在过去的半个小时里一直盯着 VS 中的“新项目”对话框...您到底是如何组织单元测试的?
我通过标准 VS 单元测试看到了这一点项目类型,它为单元测试创建一个单独的项目。使用 NUnit 时我应该这样做吗?或者应该将测试放置在与正在测试的代码相同的项目中?
我发现的“单元测试入门...”类型的教程似乎都没有解决这个问题。
So, I'm biting the bullet and trying to get started with asp.net MVC, unit testing and TDD.
I have a vague understanding of the concepts involved, which is to say somewhat beyond the "Hello World" level, but still pretty green. I'm ready for the rubber to meet the road, but
I've found myself staring at the "New Project" dialog in VS for the last half hour... how, exactly, do you organize your unit tests?
I see that with the standard VS Unit Test project type, it creates a separate project for the unit tests. Is that how I should proceed when using NUnit? Or should the tests be placed in the same project as the code being tested?
None of the "getting started with unit testing..." type tutorials I've found seem to address this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
测试应该在单独的项目中维护,因为您不想将测试代码部署到生产环境。对于单一项目解决方案,一个测试项目可能就足够了。
在内部,测试项目可以按照您认为方便的任何方式进行组织,只要保持一致即可。 ASP.NET MVC 测试项目可能有一个 ControllerTests 文件夹,每个控制器有一个测试 .cs 文件,在某种程度上反映了 MVC 项目结构。这使得测试很容易找到并与他们正在测试的代码相关联。
Tests should be maintained in separate projects because you don't want to deploy testing code to a production environment. For a single-project solution, one test project is probably sufficient.
Internally, a test project can be organized in any way you find convenient, as long as it's consistent. An ASP.NET MVC test project might have a ControllerTests folder with one test .cs file per controller, mirroring the MVC project structure to some degree. This makes the tests easy to find and relate to the code they're testing.
我组织单元测试来反映我的项目结构。例如,如果在我的 ASP.NET MVC 项目中我有
...
我的单元测试项目中有相同的文件夹。然后对于 MVC 项目中的每个文件我都有一个相应的单元测试。以下是我编写的示例项目结构的示例。
I organize my unit tests to reflect my project structure. So fir example if in my ASP.NET MVC project I have
...
I have those same folders in my unit test project. Then for each file in the MVC project I have a corresponding unit test. Here's an example of a sample project structure I wrote.
我的格式通常是这样的:
My format has usually been like so:
更重要的是首先要了解我们需要测试什么以及有哪些设施可用于此目的。它基本上是您需要关注的应用程序中对象的行为。您有很多测试框架可供选择,例如 NUnit 等。继续构建测试项目。
It is more important to understand first that what do we need to test and what facilities are available for the purpose. It is basically the behaviour of the objects in the application you need to focus on. You have plenty of testing frameworks to choose from like NUnit Etc. Work on the structure the Test project as you carry on along with.