2011-09-18 79 views
17

我試圖從[TestMethod]方法中顯示一些信息。如何從[TestMethod] Console.WriteLine?

通常我們使用NUnit與Console.WriteLine運行正常,我們可以看到它在「輸出」窗口中的線,但在這個項目上,我們必須使用與VS2010和Console.WriteLine embebed測試工具不能運行,因爲我們什麼都看不到。

我要的是展示跟蹤消息在「輸出」窗口以這種方式或多或少:

using System; 
using System.Text; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 


namespace Test1 
{ 
    [TestClass] 
    public class TestNum1 
    { 
     [TestMethod] 
     public void Constructors() 
     { 
      for (int b = 1; b < 99; b++) { 
       Console.WriteLine(b.ToString()); // <<<<<<< This don't show on Output. 
       Assert.AreEqual(b, b); // This is only a silly sample. 
      } 
     } 
    } 
} 
+0

是的,你是對的對不起我。重複的問題,但我必須添加一個註釋:您必須在調試模式下執行您的測試,或者你看不到任何輸出 – ferpega

回答

26

你應該System.Diagnostics.Debug.WriteLine(...)

取代Console.WriteLine,你會看到在Visual輸出Studio調試輸出窗口。

編輯:剛剛發現現在這是一個重複的問題,請看這裏:

How to write to Console.Out during execution of an MSTest test

+2

嗨@Davide Piras,我改變了'Console.WriteLine'與'System.Diagnostics.Debug.WriteLine'但我還看不到任何在我的輸出窗口中。 – ferpega

+1

好的..我已經看到我必須在調試模式下執行測試,否則我什麼也看不見。 – ferpega

+0

只有在使用Debug.WriteLine時,您必須在調試模式下執行您的測試 - 如果使用Trace.WriteLine,您可以使用任何編譯風格 - 包括版本 –

6

您可以強制的Console.WriteLine()顯示通過運行帶有選項/detail:stdoutMSTest命令行。

例如:

MSTEST /testcontainer:MyDllToTest.dll /testSettings.local.TestSettings /detail:stdout 

(請注意我用/testSettings防止DLL副本(部署)在testResults目錄

17

可以使用testContextInstance.WriteLine(string s);

+1

會很高興知道應該從哪裏來。 – Akku

+5

@Akku,只需添加'public TestContext TestContext {get;組; '給你的班級。測試運行者爲你填充它。您還可以將其作爲「ClassInitialize」和「AssemblyInitialize」方法的參數接收。 – Sam

+0

正是我在找的東西!輸出的文本直接顯示在測試結果窗口中 - 完美! – Shawson