Stepping through and debugging code in Unit tests
Asked Answered
H

8

64

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);
        }

    }
}
Hobnail answered 5/11, 2010 at 5:3 Comment(1)
You mean besides clicking to the right of the line, or right click breakpoint -> insert breakpoint?Reassure
P
107

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.

Precession answered 16/4, 2012 at 8:5 Comment(3)
In VS2012 We can do debug step by step by putting a Break Point in a Specific Test Method. After Go to Menu > Test > Debug > Selected Tests Or All Test. No you should able to debug. Just an Update :)Tin
In addition to @RJK's comment, this is also the same steps for stepping through tests in VS 2013 (or use Ctrl + T)Theoretician
Hm. I don't understand the reasoning behind this workflow. A unit test project builds into a dll (not an exe), so it can't be set as a startup project; and yet, it only runs in Visual Studio, and it is "ran" (not linked against). So the "solution" is to have a completely different run / debugging setup? Just out of curiosity, why didn't Microsoft make unit tests build into an application instead of a dll, then reflect for test methods? This seems like a really odd workaround for not being able to really "run" a dll.Iodoform
R
27

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.

Enter image description here

Requirement answered 4/7, 2016 at 7:28 Comment(1)
Keep right-clicking and selecting Debug is very inconvenient. If I click the green triangle or press F5 with regular project, it starts debugging and stops at break points. Why should the behaviour be different for unit test projects? That is, why can't VS stop at the breakpoint when I pressed the green button with "Debug" selected in the drop-down?Samoyedic
D
12

Yes you can, thank you :)

To actually break on them you need to run your unit tests in Debug mode though.

Denn answered 5/11, 2010 at 13:3 Comment(2)
Menu > Test > Debug > Tests in current context (Ctrl + R, Ctrl + T in VS 2010)Depositary
This answer lacks enough detail. Can you provide specific instructions? Is this different from the other answers?Amberjack
O
6

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.

Orpha answered 24/12, 2014 at 1:2 Comment(1)
Why did you respond to a 4 year old post with an answer that's basically identical to another answer?Complex
F
4

If you were running NUnit, that was so easy:

  1. Run NUnit and open your desired assembly in it.
  2. Open Visual StudioDebugAttach to Process...
  3. Select the process of NUnit
  4. Put a breakpoint in each line you want.
  5. Go back to NUnit and run tests
  6. You will see that execution stops at breakpoints
Fusionism answered 6/11, 2010 at 5:58 Comment(0)
L
4

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

Littman answered 4/10, 2011 at 11:9 Comment(0)
S
4

Two simple steps to debug a unit test in Visual Studio:

  1. Set a breakpoint in the unit test that you want to debug
  2. Right click anywhere within the unit test and select "Debug Tests" from the context menu

Stepping through the unit test is very similar to how we step through any other code in Visual Studio.

  • Step Over - F10
  • Step Into - F11
  • Step Out = Shift + F11

You can also debug the unit test from the test explorer window

  1. First locate the unit test that you want to debug
  2. Double clicking on a unit test will open that unit test
  3. Set a break point in the unit test
  4. In the test explorer, right click on that unit test and select "Debug selected tests from the context menu"

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.

Skulduggery answered 16/1, 2020 at 10:55 Comment(0)
L
0

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

Listed answered 5/11, 2010 at 5:45 Comment(1)
TestDriven is so old now - does it provide any features the in-built test explorer doesn't?Morril

© 2022 - 2024 — McMap. All rights reserved.