在单元测试中单步执行和调试代码

发布于 2024-10-01 05:51:49 字数 1311 浏览 3 评论 0原文

我无法调试或单步执行单元测试。

这是我的示例测试代码...

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

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

↘人皮目录ツ 2024-10-08 05:51:49

使用Microsoft.VisualStudio.TestTools.UnitTesting时,进入VS 2010主菜单“测试”,点击子菜单“调试”->“测试”。 “在当前上下文中进行测试”。

右键单击测试代码并选择“运行测试”将永远不会启动调试器,即使模式 = 调试也是如此。

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.

层林尽染 2024-10-08 05:51:49

在 Visual Studio 2013 中要简单得多。在测试资源管理器中,选择要调试的测试,右键单击,然后选择调试所选测试。

输入图像描述这里

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

难得心□动 2024-10-08 05:51:49

是的,你可以,谢谢:)

要真正打破它们,你需要在调试模式下运行单元测试。

Yes you can, thank you :)

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

仄言 2024-10-08 05:51:49

另一个解决方案...

您需要运行并附加调试器。

将此行设置在测试中执行的第一行(可能在测试类构造函数中):

System.Diagnostics.Debugger.Launch();

然后,当调试窗口打开时,选择 Visual Studio。

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.

烟酒忠诚 2024-10-08 05:51:49

如果您正在运行 NUnit,那就太简单了:

  1. 运行 NUnit 并在其中打开所需的程序集。
  2. 打开Visual Studio调试附加到进程...
  3. 选择NUnit的进程
  4. 在每个进程中打断点你想要的线。
  5. 返回 NUnit 并运行测试
  6. 您将看到执行在断点处停止

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
浪推晚风 2024-10-08 05:51:49

也许简单地调试测试和设置断点可以在某些类型的单元测试中起作用,但如果您调试(例如)Web 服务,则不行。

要调试 Web 服务(在单元测试中进行中断),您必须插入以下代码:

System.Diagnostics.Debugger.Break();

这将显示一个弹出窗口,指出应用程序已停止工作,您可以选择对其进行调试。

更多这里:
http://msdn.microsoft.com/en-us/library/ ms243172.aspx#DebuggingOnCassini

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

萌逼全场 2024-10-08 05:51:49

在 Visual Studio 中调试单元测试的两个简单步骤:

  1. 在要调试的单元测试中设置断点
  2. 右键单击​​单元测试中的任意位置,然后从上下文菜单中选择“调试测试”

单步执行单元测试与我们如何在 Visual Studio 中单步执行任何其他代码。

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

您还可以从以下位置调试单元测试测试资源管理器窗口

  1. 首先找到要调试的单元测试
  2. 双击单元测试将打开该单元测试
  3. 在单元测试中设置断点
  4. 在测试资源管理器中,右键单击该单元测试并选择“调试选定的测试”从上下文菜单“

调试所有测试
单击测试 - 调试 - 所有测试
然后,执行将在所有单元测试中的所有断点处暂停。

要记住的一件事是,如果您在 Visual Studio 中选择运行测试而不是调试测试,则单元测试中的断点将被忽略。

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.

国产ˉ祖宗 2024-10-08 05:51:49

一种选择是安装 TestDriven.net 这使得在任何主要单元测试上运行单元测试变得更加容易.NET 框架(NUnitxUnit、Visual Studio 工具等)。

安装后,您可以右键单击某个函数并选择测试调试器

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文