Reputation: 14148
I have not been able to debug or step through unit test.
Here is my sample test code...
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DomainModel.Entities;
using DomainModel.Abstract;
using WebUI.Controllers;
namespace Tests
{
[TestClass]
public class PeopleControllerTests
{
static IPeopleRepository MockPeopleRepository(params Person[] people)
{
var mockPeopleRepos = new Moq.Mock<IPeopleRepository>();
mockPeopleRepos.Setup(x => x.People).Returns(people.AsQueryable());
return mockPeopleRepos.Object;
}
[TestMethod]
public void Count_Of_People()
{
IPeopleRepository repository = MockPeopleRepository(
new Person { Age = 31, Gender = "Male", Name = "Tom" },
new Person { Age = 25, Gender = "Female", Name = "Sally" },
new Person { Age = 18, Gender = "Female", Name = "John" }
);
PeopleController controller = new PeopleController(repository);
var people = controller.List().ViewData.Model;
var peoplelist = people as IList<Person>;
Assert.AreEqual(3, peoplelist.Count);
}
}
}
Upvotes: 64
Views: 85320
Reputation: 137
Two simple steps to debug a unit test in Visual Studio:
Stepping through the unit test is very similar to how we step through any other code in Visual Studio.
You can also debug the unit test from the test explorer window
To debug all the tests Click Test - Debug - All Tests The execution will then pause at all the break points in all the unit tests
One thing to keep in mind is that, the break points with in the unit tests will be ignored, if you select run tests instead of debug tests in visual studio.
Upvotes: 4
Reputation: 3995
It's far simpler in Visual Studio 2013. In Test Explorer, select the tests you wish to debug, right-click, and choose debug selected tests.
Upvotes: 28
Reputation: 131
Another solution...
You need to run and attach the debugger.
Set this line at the first line executed in your test (maybe in the test class constructor):
System.Diagnostics.Debugger.Launch();
Then when the debug window is open, chose Visual Studio.
Upvotes: 6
Reputation: 10002
Maybe simply debugging tests and setting breakpoints works in some kinds of unit tests, but it doesn't if you debug, e.g., a Web service.
To debug a Web service (break inside a Unit test) you have to insert this code:
System.Diagnostics.Debugger.Break();
This will show a popup saying the application stopped working and you can choose to debug it.
More here: http://msdn.microsoft.com/en-us/library/ms243172.aspx#DebuggingOnCassini
Upvotes: 4
Reputation: 11483
If you were running NUnit, that was so easy:
Upvotes: 4
Reputation: 52073
One option is to install TestDriven.net which makes it easier to run unit tests on any of the major unit testing .NET frameworks (NUnit, xUnit, Visual Studio tools, etc.).
Once installed, you can right click on a function and choose Test With → debugger.
Upvotes: 0
Reputation: 1086
When using Microsoft.VisualStudio.TestTools.UnitTesting, go to 'Test' in the main menu of VS 2010, click submenu 'Debug' -> 'tests in current context'.
Right-clicking on the test-code and selecting 'run tests' will never start the debugger, not even when mode = debug.
Upvotes: 107
Reputation: 36649
Yes you can, thank you :)
To actually break on them you need to run your unit tests in Debug mode though.
Upvotes: 12