MVC .NET如何在现有源代码中添加单元测试项目?

时间:2023-01-19 00:16:19

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问我“你想为这个应用程序创建单元测试项目吗?”我选择不..

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?

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

4 个解决方案

#1


11  

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:

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

  • Create a test project when you add a test. When you create a new test, you can create new a test project into which the test will be added, or add the test to an existing test project that is already loaded.
  • 添加测试时创建测试项目。创建新测试时,可以创建新的测试项目,将测试添加到该测试项目中,或者将测试添加到已加载的现有测试项目中。
  • Create a test project when you generate a unit test. When you generate unit tests, you can create a new test project into which the tests will be added, or add them to an existing test project.
  • 生成单元测试时创建测试项目。生成单元测试时,可以创建一个新的测试项目,将测试添加到该测试项目中,或将它们添加到现有的测试项目中。
  • Using the Add New Project dialog box. With this method, you use a dialog box to choose the programming language of your test project.
  • 使用“添加新项目”对话框。使用此方法,您可以使用对话框选择测试项目的编程语言。

#2


6  

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)

#3


3  

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解决方案,这次选择“是”,然后将其复制到项目中。您必须更新引用以正确指向您的项目当然..

#4


0  

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

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

  1. Right click on your solution in the Solution Explorer.
  2. 在Solution Explorer中右键单击您的解决方案。
  3. Click "Add" -> "New Project".
  4. 单击“添加” - >“新建项目”。
  5. 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".
  6. 从模板列表中选择“测试”,然后选择“单元测试项目”。命名您的项目(我听说脚手架只有在您命名测试项目时才会起作用:“{YourProjectName} .Tests”),然后单击“确定”。
  7. Your project will be created with a default unit test class file, where you can put your tests, OR:
  8. 您的项目将使用默认的单元测试类文件创建,您可以在其中放置测试,或者:
  9. 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.
  10. 您可以右键单击主项目中的任何方法,然后选择“创建单元测试”并选择新创建的“单元测试项目”作为单元测试所在的位置。
  11. Depending on your project type, you may also need to add references to the various assemblies, I found this answer to be helpful: https://*.com/a/26130164/6762843
  12. 根据您的项目类型,您可能还需要添加对各种程序集的引用,我发现这个答案很有帮助:https://*.com/a/26130164/6762843
  13. 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.
  14. 并且,如果您收到错误消息“建立与SQL Server的连接时出现特定于实例的错误”,则表示您需要将App.config文件添加到测试项目并从主项目中复制连接字符串。

#1


11  

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:

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

  • Create a test project when you add a test. When you create a new test, you can create new a test project into which the test will be added, or add the test to an existing test project that is already loaded.
  • 添加测试时创建测试项目。创建新测试时,可以创建新的测试项目,将测试添加到该测试项目中,或者将测试添加到已加载的现有测试项目中。
  • Create a test project when you generate a unit test. When you generate unit tests, you can create a new test project into which the tests will be added, or add them to an existing test project.
  • 生成单元测试时创建测试项目。生成单元测试时,可以创建一个新的测试项目,将测试添加到该测试项目中,或将它们添加到现有的测试项目中。
  • Using the Add New Project dialog box. With this method, you use a dialog box to choose the programming language of your test project.
  • 使用“添加新项目”对话框。使用此方法,您可以使用对话框选择测试项目的编程语言。

#2


6  

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)

#3


3  

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解决方案,这次选择“是”,然后将其复制到项目中。您必须更新引用以正确指向您的项目当然..

#4


0  

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

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

  1. Right click on your solution in the Solution Explorer.
  2. 在Solution Explorer中右键单击您的解决方案。
  3. Click "Add" -> "New Project".
  4. 单击“添加” - >“新建项目”。
  5. 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".
  6. 从模板列表中选择“测试”,然后选择“单元测试项目”。命名您的项目(我听说脚手架只有在您命名测试项目时才会起作用:“{YourProjectName} .Tests”),然后单击“确定”。
  7. Your project will be created with a default unit test class file, where you can put your tests, OR:
  8. 您的项目将使用默认的单元测试类文件创建,您可以在其中放置测试,或者:
  9. 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.
  10. 您可以右键单击主项目中的任何方法,然后选择“创建单元测试”并选择新创建的“单元测试项目”作为单元测试所在的位置。
  11. Depending on your project type, you may also need to add references to the various assemblies, I found this answer to be helpful: https://*.com/a/26130164/6762843
  12. 根据您的项目类型,您可能还需要添加对各种程序集的引用,我发现这个答案很有帮助:https://*.com/a/26130164/6762843
  13. 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.
  14. 并且,如果您收到错误消息“建立与SQL Server的连接时出现特定于实例的错误”,则表示您需要将App.config文件添加到测试项目并从主项目中复制连接字符串。