asp.net-mvc MVC .NET 如何为现有源代码添加单元测试项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2256932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:08:04  来源:igfitidea点击:

MVC .NET How to add unit test project in for existing source code?

asp.net-mvcunit-testingvisual-studio-2008

提问by aifarfa

I've already created MVC project (VS2008 .NET 3.5) at first time VS2008 ask me "Would you like to create Unit test project for this application ?" I choose No..

我已经在第一次创建 MVC 项目 (VS2008 .NET 3.5) VS2008 问我“你想为这个应用程序创建单元测试项目吗?” 我选择No..

Now I realize that it's growing more complex and TDD (test driven development) approach would have been better.

现在我意识到它变得越来越复杂,TDD(测试驱动开发)方法会更好。

but can't find out how to add a unit test project in my solution?

但找不到如何在我的解决方案中添加单元测试项目?

采纳答案by Adriaan Stander

Maybe this will help you

也许这会帮助你

How to: Create a Test Project

如何:创建测试项目

As described in the following procedures, you can create test projects in several ways:

如以下过程所述,您可以通过多种方式创建测试项目:

回答by jeroenh

Just use Add -> New Project

只需使用添加 -> 新建项目

Either add a simple Class Library, and manually add a reference to the unit test framework of your choice, or use the "Unit test project" template that comes with visual studio (for use with MSTest)

添加一个简单的类库,并手动添加对您选择的单元测试框架的引用,或者使用 Visual Studio 附带的“单元测试项目”模板(与 MSTest 一起使用)

回答by markt

If I remember correctly, the generated unit test project has some example tests for the default project controllers, etc..

如果我没记错的话,生成的单元测试项目有一些针对默认项目控制器等的示例测试。

If you want to use the generated unit test project as a starting point, you could always create a new ASP.NET MVC solution, select 'Yes' this time, and then copy that into your project. You'll have to update the references to correctly point to your project of course..

如果您想使用生成的单元测试项目作为起点,您始终可以创建一个新的 ASP.NET MVC 解决方案,这次选择“是”,然后将其复制到您的项目中。当然,您必须更新引用以正确指向您的项目。

回答by karink

I have used the "Add -> New Project" method:

我使用了“添加 - >新建项目”方法:

  1. Right click on your solution in the Solution Explorer.
  2. Click "Add" -> "New Project".
  3. Select "Test" from the template list, and then select "Unit Test Project". Name your Project (I have heard that scaffolding will only work if you name your test project: "{YourProjectName}.Tests"), and click "OK".
  4. Your project will be created with a default unit test class file, where you can put your tests, OR:
  5. You can right click any of the methods in your main project and select "Create Unit Tests" and select your newly created "Unit Test Project" as the place where your unit tests will live.
  6. Depending on your project type, you may also need to add references to the various assemblies, I found this answer to be helpful: https://stackoverflow.com/a/26130164/6762843
  7. And, if you get an error saying "instance-specific error occurred while establishing a connection to SQL Server" that means that you need to add an App.config file to your test project and copy the connection string from your main project.
  1. 在解决方案资源管理器中右键单击您的解决方案。
  2. 单击“添加”->“新建项目”。
  3. 从模板列表中选择“Test”,然后选择“Unit Test Project”。为您的项目命名(我听说只有将测试项目命名为“{YourProjectName}.Tests”时脚手架才会起作用),然后单击“确定”。
  4. 您的项目将使用默认的单元测试类文件创建,您可以在其中放置您的测试,或者:
  5. 您可以右键单击主项目中的任何方法并选择“创建单元测试”,然后选择新创建的“单元测试项目”作为单元测试所在的位置。
  6. 根据您的项目类型,您可能还需要添加对各种程序集的引用,我发现这个答案很有帮助:https: //stackoverflow.com/a/26130164/6762843
  7. 而且,如果您收到错误消息“在建立与 SQL Server 的连接时发生特定于实例的错误”,则意味着您需要将 App.config 文件添加到您的测试项目并从您的主项目复制连接字符串。