在单元测试中单步执行和调试代码
我无法调试或单步执行单元测试。
这是我的示例测试代码...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用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.
在 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.
是的,你可以,谢谢:)
要真正打破它们,你需要在调试模式下运行单元测试。
Yes you can, thank you :)
To actually break on them you need to run your unit tests in Debug mode though.
另一个解决方案...
您需要运行并附加调试器。
将此行设置在测试中执行的第一行(可能在测试类构造函数中):
然后,当调试窗口打开时,选择 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):
Then when the debug window is open, chose Visual Studio.
如果您正在运行 NUnit,那就太简单了:
If you were running NUnit, that was so easy:
也许简单地调试测试和设置断点可以在某些类型的单元测试中起作用,但如果您调试(例如)Web 服务,则不行。
要调试 Web 服务(在单元测试中进行中断),您必须插入以下代码:
这将显示一个弹出窗口,指出应用程序已停止工作,您可以选择对其进行调试。
更多这里:
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:
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
在 Visual Studio 中调试单元测试的两个简单步骤:
单步执行单元测试与我们如何在 Visual Studio 中单步执行任何其他代码。
您还可以从以下位置调试单元测试测试资源管理器窗口
调试所有测试
单击测试 - 调试 - 所有测试
然后,执行将在所有单元测试中的所有断点处暂停。
要记住的一件事是,如果您在 Visual Studio 中选择运行测试而不是调试测试,则单元测试中的断点将被忽略。
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.
一种选择是安装 TestDriven.net 这使得在任何主要单元测试上运行单元测试变得更加容易.NET 框架(NUnit、xUnit、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 With → debugger.