18

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..

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

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

4 Answers 4

12

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:

Sign up to request clarification or add additional context in comments.

2 Comments

How do I add individual test methods to actions? There is no "Add test method" in the context menu like I'm used to in Windows app development.
@Shimmy you can add a unit test for a given controller action in VS 2015 by right-clicking on the action name and selecting Create Unit Tests from the context menu. The question does mention VS 2008, but I'm not sure if that feature was available at that time.
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)

1 Comment

I am beginner and I already have a complex web site built. And I need to unit test it. Can you kindly tell me the steps. Or pass on any link which I can follow along. I don't know about class libraries or "test framework of your choice". Is there any material for beginner which I can follow along. Help would be highly appreciated.
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..

1 Comment

That is an idea I have come up with too.
1

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 Comment

Thank you. It never worked for me (or allowed me to create a reference) if I didn't name it that way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.