在 VS 2008 中调试单元测试用例

发布于 2024-07-26 05:14:45 字数 173 浏览 3 评论 0原文

我正在尝试在 Visual Studio 2008 中调试一些单元测试,并注意到断点似乎并没有停止执行。

我认为这就像设置断点然后执行“测试|调试|在当前上下文中测试”一样简单......但这实际上从未达到我设置的断点。

我做错了什么还是这只是坏了?

谢谢, 布兰登

I'm trying to debug some of my unit tests in Visual Studio 2008 and have noticed that breakpoints don't seem to be halting execution.

I kind of assumed that it was as simple as setting a breakpoint and then doing "Test | Debug | Tests in current context" ... but this never actually hits the breakpoints that I've set.

Am I doing something wrong or is this just broken?

Thanks,
Brandon

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

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

发布评论

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

评论(5

何以畏孤独 2024-08-02 05:14:45

我遇到了同样的问题,直到我先手动附加到 aspnet_wp.exe 进程,然后单击“调试测试”按钮。 然后我的断点终于被命中了。

I had this same problem until I manually attached to the aspnet_wp.exe process first and then clicked on the Debug Tests buttons. Then my breakpoints were finally hit.

温柔少女心 2024-08-02 05:14:45

就我而言,System.Diagnostics.Debugger.Break() 不会停止于测试方法。

[TestClass]
public class ContactListTest
{
    #region "Constants"
    public const string COVERAGE = "CoverageService";

    public const string CompanyList = "CompanyList";
    public const string ContactList = "ContactList";
    #endregion

    [TestMethod]
    public void GetContactListTest()
    {
        System.Diagnostics.Debugger.Break();

        var ex = new ServiceFilterExpression(COVERAGE);
        ex.Expression = new OpBeginsWith("Type", ContactList);
        var result = ex.FetchData();

   } 
}

In my case System.Diagnostics.Debugger.Break() doesn't stop at testing method.

[TestClass]
public class ContactListTest
{
    #region "Constants"
    public const string COVERAGE = "CoverageService";

    public const string CompanyList = "CompanyList";
    public const string ContactList = "ContactList";
    #endregion

    [TestMethod]
    public void GetContactListTest()
    {
        System.Diagnostics.Debugger.Break();

        var ex = new ServiceFilterExpression(COVERAGE);
        ex.Expression = new OpBeginsWith("Type", ContactList);
        var result = ex.FetchData();

   } 
}
生寂 2024-08-02 05:14:45

如果您使用 nUnit,则必须使用

要测试的 DLL 启动 Nunit。
然后在 Visual Studio 中转到
工具-> 附加到进程

选择您的 nunit 进程并单击“附加”,然后它将在您的所有断点处停止,

玩得开心:-)

if you use nUnit you have to do following

start Nunit with the DLL you want to test.
then in Visual Studio go to
Tools -> Attach to Process

choose your nunit process and click "Attach" then it will halt in all your breakpoints

have fun :-)

怎会甘心 2024-08-02 05:14:45

Microsoft 官方 workaround/kludge/zomg-I-can't- believe-they -can't-be-arsed-to-provide-this-after-4-years 对于 VS2010、VS2008 和 VS2005 中的 MSTEST 是添加 System.Diagnostics.Debugger.Break() code> 到您想要开始调试的单元测试。 这适用于具有单元测试项目引用的调试符号的所有项目。

.NET 运行时将提示您转储到调试模式(或关闭正在执行的单元测试程序,或忽略调试行),并且(有时)允许您使用启动单元测试的 Visual Studio 实例来执行此操作。 您始终可以从新的 VS 实例进行调试。 一旦到达 System.Diagnostics.Debugger.Break() 行,所有其他断点都将处于活动状态并被命中(假设它们位于执行堆栈中)。

The official Microsoft workaround/kludge/zomg-I-can't-believe-they-can't-be-arsed-to-provide-this-after-4-years for MSTEST in VS2010, VS2008, and VS2005 is to add System.Diagnostics.Debugger.Break() to the unit test you want to begin debugging from. This works for all projects with debug symbols referenced by the unit test project.

The .NET runtime will prompt you to dump into debug mode (or close the executing unit test program, or ignore the debug line), and (sometimes) allow you to use instance of visual studio that launched the unit test to do so. You can always debug from a new VS instance. Once you hit that System.Diagnostics.Debugger.Break() line, all other breakpoints will be active and hit (assuming they're in the execution stack).

£冰雨忧蓝° 2024-08-02 05:14:45

检查以下内容:

  • 测试是否标记有 [TestClass][TestMethod]
  • 您正在运行调试模式还是发布模式构建? (除了确实发生的情况外,不会产生巨大的差异)调试更好。
  • 您的编译是否经过优化? 没有更好
  • 尝试在解决方案中运行所有测试来检查是否到达断点
  • ,最后,也许您有错误,这就是为什么您没有到达代码?

Check the following:

  • Are the tests marked with [TestClass] and [TestMethod]?
  • Are you running Debug or Release mode builds? (Doesn't make a huge difference except when it does) Debug is better.
  • Are you compiling with or without optimizations? Without is better
  • Try to run All Tests in Solution in check if you hit the breakpoints
  • and lastly, maybe you have bug and that's why you are not hitting the code?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文