Visual Studio 测试中的跟踪(从 NUnit 迁移)
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
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.通常我使用这种方法在 Visual Studio 的输出窗口中打印一些内容:
Usually I use this method to print something in the output window of visual studio:
要查看结果,请双击“测试结果”窗口中的测试(可从主菜单项“测试”>> 窗口菜单>> 测试结果访问)
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)
所有早期的答案实际上都是正确的,但或多或少需要点击鼠标。
如果您希望立即查看输出而无需额外点击,只需添加列调试跟踪和/或输出(标准输出)(无论是通过右键单击测试结果,然后选择“添加/删除列”,将您使用 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'.