Visual Studio 测试中的跟踪(从 NUnit 迁移)

发布于 2024-07-30 09:55:54 字数 1229 浏览 7 评论 0原文

在 NUnit 中,我习惯在测试中编写 Trace 语句,并将它们显示在 NUnit gui 的跟踪选项卡中。

在一个新项目中,我将转向 Visual Studio Professional Addition 中的内置单元测试,我认为它是 mstest.exe 的接口。

测试代码:

<TestMethod()>
Public Sub TestPagesInheritFromBasePage()
    Dim webUI As Assembly = Assembly.GetAssembly(GetType(WebUI.BasePage))
    Dim badPages As New List(Of String)
    For Each t As Type In webUI.GetTypes()
        Debug.Write(t.Name + ", ")
        Trace.Write(t.Name + ", ")
        If t.BaseType Is GetType(System.Web.UI.Page) Then badPages.Add(t.Name)
    Next
    Debug.Flush()
    Trace.Flush()
    If badPages.Count > 0 Then
        Assert.Fail("{0}: do not inheriting from BasePage", String.Join(", ", badPages.ToArray()))
    End If
End Sub

我遇到了失败,所以我知道 Debug.Write 和 Trace.Write 行正在执行。

我已经阅读了有关编写这些测试的 MSDN 文档,并且如果在命令行执行,我可以通过以下方式查看跟踪输出:

mstest.exe /testcontainer:mydll.dll /detail:debugtrace

但是,直接在 Visual Studio 中执行测试时,我找不到跟踪输出。 在单元测试期间是否有另一种输出信息的首选方法,或者我是否缺少在 Visual Studio 中查看跟踪信息的选项?

答案: 下面的两个答案(Console.Write 和 Debug.Write)都有效,结果位于测试结果详细信息中(底部的 TestResult 窗格,右键单击测试结果并转到 TestResultDetails)。 另外,我在项目属性中设置了“调试”和“跟踪”常量。

In NUnit, I'm used to writing Trace statements in the test, and having them show up in the trace tab of the NUnit gui.

On a new project, I'm moving to the built in Unit Testing in Visual Studio Professional Addition, which I believe is an interface to mstest.exe.

Test Code:

<TestMethod()>
Public Sub TestPagesInheritFromBasePage()
    Dim webUI As Assembly = Assembly.GetAssembly(GetType(WebUI.BasePage))
    Dim badPages As New List(Of String)
    For Each t As Type In webUI.GetTypes()
        Debug.Write(t.Name + ", ")
        Trace.Write(t.Name + ", ")
        If t.BaseType Is GetType(System.Web.UI.Page) Then badPages.Add(t.Name)
    Next
    Debug.Flush()
    Trace.Flush()
    If badPages.Count > 0 Then
        Assert.Fail("{0}: do not inheriting from BasePage", String.Join(", ", badPages.ToArray()))
    End If
End Sub

I'm getting a failure, so I know the Debug.Write and Trace.Write lines are executing.

I've read through the MSDN docs on writing these tests, and I can view the trace output if executing at the command line, via:

mstest.exe /testcontainer:mydll.dll /detail:debugtrace

However, I can not find the trace output when executing the tests directly in visual studio. Is there another preferred method of outputting information during a unit test, or am I missing an option to see trace info in visual studio?

Answer:
Both of the answers below (Console.Write and Debug.Write) worked, the results were in Test Results Detail (TestResult Pane at the bottom, right click on on Test Results and go to TestResultDetails). Also, I set Debug and Trace constants in project properties.

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

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

发布评论

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

评论(4

七颜 2024-08-06 09:55:55

尝试使用 Console.WriteLine() 代替。 我在单元测试中使用它,它工作得很好 - 它在单元测试结果输出窗口中显示文本。

Try using Console.WriteLine() instead. I use that in my unit tests and it works fine - it displays text in unit test result output window.

泼猴你往哪里跑 2024-08-06 09:55:55

通常我使用这种方法在 Visual Studio 的输出窗口中打印一些内容:

System.Diagnostics.Debug.WriteLine("Message");

Usually I use this method to print something in the output window of visual studio:

System.Diagnostics.Debug.WriteLine("Message");
独守阴晴ぅ圆缺 2024-08-06 09:55:55

要查看结果,请双击“测试结果”窗口中的测试(可从主菜单项“测试”>> 窗口菜单>> 测试结果访问)

To see the results double click on the test in the "Test Results" window (Accessed from the main menu item "Tests" >> window menu >> Test Results)

摇划花蜜的午后 2024-08-06 09:55:55

所有早期的答案实际上都是正确的,但或多或​​少需要点击鼠标。

如果您希望立即查看输出而无需额外点击,只需添加列调试跟踪和/或输出(标准输出)(无论是通过右键单击测试结果,然后选择“添加/删除列”,将您使用 Debug.Write 或 Console.Write)添加到测试结果窗格中。

All earlier answers are actually correct but require more or less mouse-clicking.

If you would like to see the output immediately without an extra click, just add the columns Debug Trace and/or Output (StdOut) (whether you're using Debug.Write or Console.Write) to the Test Results pane through right-clicking on the test result and then 'Add/Remove Columns'.

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